<?php
// 允许来自任何源的请求
header('Access-Control-Allow-Origin: *');
// 允许所有HTTP方法
header('Access-Control-Allow-Methods: *');
// 允许所有请求头
header('Access-Control-Allow-Headers: *');
// 如果是预检请求，返回状态码 200
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Max-Age: 86400');
    exit(0);
}

include_once __DIR__ . '/../vendor/autoload.php';

//丢嗨，点知唔得行
include_once __DIR__ . '/extend/aliyun-oss-php-sdk-2.5.0.phar';


spl_autoload_register(function ($class_name) {
    // 将完整的命名空间名称转换为相应的目录结构
    $base_dir = __DIR__ . '/'; // 使用当前目录作为基础目录
    $class_name = str_replace("\\", DIRECTORY_SEPARATOR, $class_name);
    // 构建完整的文件路径
    $file = $base_dir . $class_name . '.php';
    $file = str_replace('api/api/', 'api/', $file);

    // 检查文件是否存在，如果存在则包含它
    if (file_exists($file)) include_once $file;
});

// 设置错误处理器，将所有错误转换为异常
set_error_handler(/**
 * @throws ErrorException
 */ function ($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
});

use api\common\controller\ReturnController;
use api\controller\v1\logic\UserLogic;
use config\Constant;
use extend\Common;
use think\facade\Db;

try {
    // 初始化ThinkPHP数据库
    Db::setConfig(Constant::database['mysql']);

    $parts = explode('/', trim($_GET['path'] ?? '', '/'));
    $method = array_pop($parts);               // 获取并移除最后一个元素，即方法名
    $method = Common::dotToCamelCase($method); // 将方法名从小数点形式转换为驼峰命名
    $className = array_pop($parts);            // 获取并移除倒数第二个元素，即类名
    $className = Common::dotToCamelCase($className); // 将方法名从小数点形式转换为驼峰命名
    $namespacePath = implode('\\', $parts);    // 使用剩余的部分构建命名空间路径

    $fullyQualifiedClassName = $namespacePath . "\\" . ucfirst($className);
    if (!class_exists($fullyQualifiedClassName)) {
        ReturnController::error('文件类型不存在。', $fullyQualifiedClassName);
    }
    if (!method_exists($fullyQualifiedClassName, $method)) {
        ReturnController::error('公网API未部署，请查看文档。');
    }

    $classReflection = new ReflectionClass($fullyQualifiedClassName);
    if (Common::hasAnnotation($classReflection, '@privateApi')) {
        ReturnController::error('该类为非公网API，请查看文档。');
    }
    $token = null;
    $reflection = new ReflectionMethod($fullyQualifiedClassName, $method);
    if (!Common::hasAnnotation($classReflection, '@notLogin') && !Common::hasAnnotation($reflection, '@notLogin')) {
        if (!(new UserLogic())->isLogin()) {
            ReturnController::error('请先登录', 401,code: 401);
        }
    }

    if (!$reflection->isPublic()) {
        ReturnController::error('非公网API，请查看文档。');
    }
    //方法 - 私有API
    if (Common::hasAnnotation($reflection, '@privateApi')) {
        ReturnController::error('该方法为非公网API，请查看文档。');
    }

    $instance = new $fullyQualifiedClassName();
    call_user_func([$instance, $method]);

} catch (ErrorException $e) {
    error_log($e->getMessage());
    ReturnController::error($e->getMessage(), $e);
} catch (Throwable $t) {
    error_log($t->getMessage());
    ReturnController::error($t->getMessage(), $t);
}