<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;

// ========== 系统路由 ==========
/* Route::get('/', function () {
    return json([
        'app' => '进销存数据开放平台',
        'version' => '1.0.0',
        'docs' => '/docs',
    ]);
}); */

Route::get('/health', function () {
    try {
        \think\facade\Db::connect('mssql')->query('SELECT 1');
        $mssqlStatus = 'connected';
    } catch (\Exception $e) {
        $mssqlStatus = 'disconnected';
    }

    return json([
        'status' => $mssqlStatus === 'connected' ? 'ok' : 'degraded',
        'sql_server' => $mssqlStatus,
    ]);
});

// ========== 对外API路由（需要签名验证和限流）==========
Route::group('api/v1', function () {
    Route::get('chuku', 'api.v1.Chuku/index');
    Route::get('ruku', 'api.v1.Ruku/index');
})->middleware([\app\middleware\Auth::class, \app\middleware\RateLimit::class]);

// ========== 后台管理路由（需要管理员认证）==========
// 登录接口不需要认证
Route::post('api/admin/auth/login', 'api.admin.Auth/login');

// 修改密码接口需要认证
Route::put('api/admin/auth/password', 'api.admin.Auth/changePassword')->middleware([\app\middleware\AdminAuth::class]);

// 其他管理接口需要认证
Route::group('api/admin', function () {
    // 仪表盘
    Route::get('dashboard/stats', 'api.admin.Dashboard/stats');
    Route::get('dashboard/trend', 'api.admin.Dashboard/trend');

    // 客户管理 - 按从具体到通用的顺序排列
    Route::post('clients/api-keys/create/:id', 'api.admin.ClientController/generateApiKey');
    Route::get('clients/api-keys/:id', 'api.admin.ClientController/apiKeys');
    Route::put('clients/update/:id', 'api.admin.ClientController/update');
    Route::patch('clients/toggle/:id', 'api.admin.ClientController/toggle');
    Route::delete('api-keys/delete/:keyId', 'api.admin.ClientController/deleteApiKey');
    Route::get('clients', 'api.admin.ClientController/index');
    Route::post('clients', 'api.admin.ClientController/create');

    // 权限管理
    Route::get('permissions/:id', 'api.admin.PermissionController/read');
    Route::put('permissions/:id', 'api.admin.PermissionController/update');

    // 日志管理
    Route::get('logs', 'api.admin.Log/index');
    Route::get('logs/:id', 'api.admin.Log/read');
    Route::get('logs/stats', 'api.admin.Log/stats');
    Route::get('logs/export', 'api.admin.Log/export');
})->middleware([\app\middleware\AdminAuth::class]);
