<?php

namespace Yajra\Oci8\PHPStan;

use Larastan\Larastan\Reflection\StaticMethodReflection;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;

class DatabaseFacadeExtension implements MethodsClassReflectionExtension
{
    private const SUPPORTED_METHODS = [
        'getSchema',
        'setSchema',
        'setSessionVars',
        'getSequence',
        'getTrigger',
        'serverVersion',
        'isVersionAbove',
        'isVersionAboveOrEqual',
        'isVersionBelow',
        'isVersionBelowOrEqual',
        'setDateFormat',
        'getDateFormat',
        'executeFunction',
        'executeProcedure',
        'executeProcedureWithCursor',
        'createSqlFromProcedure',
        'createStatementFromProcedure',
        'createStatementFromFunction',
        'getSchemaPrefix',
        'setSchemaPrefix',
        'getMaxLength',
        'setMaxLength',
        'getSchemaState',
        'addBindingsToStatement',
        'useCaseInsensitiveSession',
        'useCaseSensitiveSession',
    ];

    public function __construct(
        private ReflectionProvider $reflectionProvider
    ) {}

    public function hasMethod(ClassReflection $classReflection, string $methodName): bool
    {
        if ($classReflection->getName() !== 'Illuminate\Support\Facades\DB') {
            return false;
        }

        if (! in_array($methodName, self::SUPPORTED_METHODS, true)) {
            return false;
        }

        // Only add these methods if the Oracle connection class exists
        try {
            $this->reflectionProvider->getClass('Yajra\Oci8\Oci8Connection');

            return true;
        } catch (ClassNotFoundException $e) {
            return false;
        }
    }

    public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
    {
        try {
            $oci8ConnectionClass = $this->reflectionProvider->getClass('Yajra\Oci8\Oci8Connection');

            if ($oci8ConnectionClass->hasNativeMethod($methodName)) {
                return new StaticMethodReflection($oci8ConnectionClass->getNativeMethod($methodName));
            }
        } catch (ClassNotFoundException $e) {
            throw new ShouldNotHappenException('Yajra\Oci8\Oci8Connection class not found');
        }

        throw new ShouldNotHappenException("Method {$methodName} not found on Oci8Connection");
    }
}
