<?php
/**
 * API入口文件
 */

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/common/Database.php';
require_once __DIR__ . '/common/Response.php';
require_once __DIR__ . '/common/Wechat.php';
require_once __DIR__ . '/common/Auth.php';
require_once __DIR__ . '/common/Utils.php';

// 路由处理
$request_uri = $_SERVER['REQUEST_URI'];
$path = parse_url($request_uri, PHP_URL_PATH);
$path = str_replace('/api', '', $path);
$path = trim($path, '/');

// 解析路由
$parts = explode('/', $path);
$module = $parts[0] ?? 'index';
$action = $parts[1] ?? 'index';

// 路由映射
$routes = [
    'user' => 'controllers/UserController.php',
    'room' => 'controllers/RoomController.php',
    'transaction' => 'controllers/TransactionController.php',
    'fee' => 'controllers/FeeController.php',
    'prize' => 'controllers/PrizeController.php',
    'admin' => 'controllers/AdminController.php',
    'config' => 'controllers/ConfigController.php',
];

if (isset($routes[$module])) {
    $controller_file = __DIR__ . '/' . $routes[$module];
    if (file_exists($controller_file)) {
        require_once $controller_file;
        
        $controller_class = ucfirst($module) . 'Controller';
        if (class_exists($controller_class)) {
            $controller = new $controller_class();
            $method = strtolower($_SERVER['REQUEST_METHOD']) . '_' . $action;
            
            if (method_exists($controller, $method)) {
                $controller->$method();
            } else {
                Response::error('方法不存在', 404);
            }
        } else {
            Response::error('控制器不存在', 404);
        }
    } else {
        Response::error('控制器文件不存在', 404);
    }
} else {
    Response::error('接口不存在', 404);
}

