<?php

use Illuminate\Support\Facades\Route;

// 公开路由（不需要认证）
Route::post('/auth/login', [\App\Http\Controllers\Auth\Admin\Auth::class, 'login']);

// 需要认证的路由
Route::middleware(['auth.check:admin', 'log.request'])->group(function () {
    // Auth 模块
    Route::prefix('auth')->group(function () {
        // 认证相关
        Route::post('/logout', [\App\Http\Controllers\Auth\Admin\Auth::class, 'logout']);
        Route::post('/refresh', [\App\Http\Controllers\Auth\Admin\Auth::class, 'refresh']);
        Route::get('/me', [\App\Http\Controllers\Auth\Admin\Auth::class, 'me']);
        Route::post('/change-password', [\App\Http\Controllers\Auth\Admin\Auth::class, 'changePassword']);

        // 用户管理
        Route::prefix('user')->group(function () {
            Route::get('/', [\App\Http\Controllers\Auth\Admin\User::class, 'index']);
            Route::get('/{id}', [\App\Http\Controllers\Auth\Admin\User::class, 'show']);
            Route::post('/', [\App\Http\Controllers\Auth\Admin\User::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\Auth\Admin\User::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\Auth\Admin\User::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\Auth\Admin\User::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\Auth\Admin\User::class, 'batchUpdateStatus']);
            Route::post('/batch-department', [\App\Http\Controllers\Auth\Admin\User::class, 'batchAssignDepartment']);
            Route::post('/batch-roles', [\App\Http\Controllers\Auth\Admin\User::class, 'batchAssignRoles']);
            Route::post('/export', [\App\Http\Controllers\Auth\Admin\User::class, 'export']);
            Route::post('/import', [\App\Http\Controllers\Auth\Admin\User::class, 'import']);
            Route::get('/download-template', [\App\Http\Controllers\Auth\Admin\User::class, 'downloadTemplate']);
        });

        // 角色管理
        Route::prefix('role')->group(function () {
            Route::get('/', [\App\Http\Controllers\Auth\Admin\Role::class, 'index']);
            Route::get('/all', [\App\Http\Controllers\Auth\Admin\Role::class, 'getAll']);
            Route::get('/{id}', [\App\Http\Controllers\Auth\Admin\Role::class, 'show']);
            Route::post('/', [\App\Http\Controllers\Auth\Admin\Role::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\Auth\Admin\Role::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\Auth\Admin\Role::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\Auth\Admin\Role::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\Auth\Admin\Role::class, 'batchUpdateStatus']);
            Route::post('/{id}/permissions', [\App\Http\Controllers\Auth\Admin\Role::class, 'assignPermissions']);
            Route::get('/{id}/permissions', [\App\Http\Controllers\Auth\Admin\Role::class, 'getPermissions']);
            Route::post('/{id}/copy', [\App\Http\Controllers\Auth\Admin\Role::class, 'copy']);
            Route::post('/batch-copy', [\App\Http\Controllers\Auth\Admin\Role::class, 'batchCopy']);
        });

        // 权限管理
        Route::prefix('permission')->group(function () {
            Route::get('/', [\App\Http\Controllers\Auth\Admin\Permission::class, 'index']);
            Route::get('/tree', [\App\Http\Controllers\Auth\Admin\Permission::class, 'tree']);
            Route::get('/menu', [\App\Http\Controllers\Auth\Admin\Permission::class, 'menu']);
            Route::get('/{id}', [\App\Http\Controllers\Auth\Admin\Permission::class, 'show']);
            Route::post('/', [\App\Http\Controllers\Auth\Admin\Permission::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\Auth\Admin\Permission::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\Auth\Admin\Permission::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\Auth\Admin\Permission::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\Auth\Admin\Permission::class, 'batchUpdateStatus']);
        });

        // 部门管理
        Route::prefix('department')->group(function () {
            Route::get('/', [\App\Http\Controllers\Auth\Admin\Department::class, 'index']);
            Route::get('/tree', [\App\Http\Controllers\Auth\Admin\Department::class, 'tree']);
            Route::get('/all', [\App\Http\Controllers\Auth\Admin\Department::class, 'getAll']);
            Route::get('/{id}', [\App\Http\Controllers\Auth\Admin\Department::class, 'show']);
            Route::post('/', [\App\Http\Controllers\Auth\Admin\Department::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\Auth\Admin\Department::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\Auth\Admin\Department::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\Auth\Admin\Department::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\Auth\Admin\Department::class, 'batchUpdateStatus']);
            Route::post('/export', [\App\Http\Controllers\Auth\Admin\Department::class, 'export']);
            Route::post('/import', [\App\Http\Controllers\Auth\Admin\Department::class, 'import']);
            Route::get('/download-template', [\App\Http\Controllers\Auth\Admin\Department::class, 'downloadTemplate']);
        });

        // 在线用户管理
        Route::prefix('online-user')->group(function () {
            Route::get('/count', [\App\Http\Controllers\Auth\Admin\User::class, 'getOnlineCount']);
            Route::get('/', [\App\Http\Controllers\Auth\Admin\User::class, 'getOnlineUsers']);
            Route::get('/{userId}/sessions', [\App\Http\Controllers\Auth\Admin\User::class, 'getUserSessions']);
            Route::post('/{userId}/offline', [\App\Http\Controllers\Auth\Admin\User::class, 'setUserOffline']);
            Route::post('/{userId}/offline-all', [\App\Http\Controllers\Auth\Admin\User::class, 'setUserAllOffline']);
        });
    });

    // 系统管理模块
    Route::prefix('system')->group(function () {
        // 系统配置管理
        Route::prefix('setting')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Config::class, 'index']);
            Route::get('/all', [\App\Http\Controllers\System\Admin\Config::class, 'getByGroup']);
            Route::get('/groups', [\App\Http\Controllers\System\Admin\Config::class, 'getGroups']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\Config::class, 'show']);
            Route::post('/', [\App\Http\Controllers\System\Admin\Config::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\System\Admin\Config::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Config::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Config::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\System\Admin\Config::class, 'batchUpdateStatus']);
        });

        // 系统操作日志
        Route::prefix('log')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Log::class, 'index']);
            Route::get('/export', [\App\Http\Controllers\System\Admin\Log::class, 'export']);
            Route::get('/statistics', [\App\Http\Controllers\System\Admin\Log::class, 'getStatistics']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\Log::class, 'show']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Log::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Log::class, 'batchDelete']);
            Route::post('/clear', [\App\Http\Controllers\System\Admin\Log::class, 'clearLogs']);
        });

        // 数据字典管理
        Route::prefix('dictionary')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Dictionary::class, 'index']);
            Route::get('/all', [\App\Http\Controllers\System\Admin\Dictionary::class, 'all']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\Dictionary::class, 'show']);
            Route::post('/', [\App\Http\Controllers\System\Admin\Dictionary::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\System\Admin\Dictionary::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Dictionary::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Dictionary::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\System\Admin\Dictionary::class, 'batchUpdateStatus']);
        });

        // 数据字典项管理
        Route::prefix('dictionary-item')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Dictionary::class, 'getItemsList']);
            Route::get('/all', [\App\Http\Controllers\System\Admin\Dictionary::class, 'getAllItems']);
            Route::post('/', [\App\Http\Controllers\System\Admin\Dictionary::class, 'storeItem']);
            Route::put('/{id}', [\App\Http\Controllers\System\Admin\Dictionary::class, 'updateItem']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Dictionary::class, 'destroyItem']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Dictionary::class, 'batchDeleteItems']);
            Route::post('/batch-status', [\App\Http\Controllers\System\Admin\Dictionary::class, 'batchUpdateItemsStatus']);
        });

        // 任务管理
        Route::prefix('task')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Task::class, 'index']);
            Route::get('/all', [\App\Http\Controllers\System\Admin\Task::class, 'all']);
            Route::get('/statistics', [\App\Http\Controllers\System\Admin\Task::class, 'getStatistics']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\Task::class, 'show']);
            Route::post('/', [\App\Http\Controllers\System\Admin\Task::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\System\Admin\Task::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Task::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Task::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\System\Admin\Task::class, 'batchUpdateStatus']);
            Route::post('/{id}/run', [\App\Http\Controllers\System\Admin\Task::class, 'run']);
        });

        // 城市数据管理
        Route::prefix('city')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\City::class, 'index']);
            Route::get('/tree', [\App\Http\Controllers\System\Admin\City::class, 'tree']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\City::class, 'show']);
            Route::get('/code/{code}/children', [\App\Http\Controllers\System\Admin\City::class, 'children']);
            Route::get('/provinces', [\App\Http\Controllers\System\Admin\City::class, 'provinces']);
            Route::get('/{provinceId}/cities', [\App\Http\Controllers\System\Admin\City::class, 'cities']);
            Route::get('/{cityId}/districts', [\App\Http\Controllers\System\Admin\City::class, 'districts']);
            Route::post('/', [\App\Http\Controllers\System\Admin\City::class, 'store']);
            Route::put('/{id}', [\App\Http\Controllers\System\Admin\City::class, 'update']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\City::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\City::class, 'batchDelete']);
            Route::post('/batch-status', [\App\Http\Controllers\System\Admin\City::class, 'batchUpdateStatus']);
        });

        // 文件上传管理
        Route::prefix('upload')->group(function () {
            Route::post('/', [\App\Http\Controllers\System\Admin\Upload::class, 'upload']);
            Route::post('/multiple', [\App\Http\Controllers\System\Admin\Upload::class, 'uploadMultiple']);
            Route::post('/base64', [\App\Http\Controllers\System\Admin\Upload::class, 'uploadBase64']);
            Route::post('/delete', [\App\Http\Controllers\System\Admin\Upload::class, 'delete']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Upload::class, 'batchDelete']);
        });

        // 通知管理
        Route::prefix('notification')->group(function () {
            Route::get('/', [\App\Http\Controllers\System\Admin\Notification::class, 'index']);
            Route::get('/unread', [\App\Http\Controllers\System\Admin\Notification::class, 'unread']);
            Route::get('/unread-count', [\App\Http\Controllers\System\Admin\Notification::class, 'unreadCount']);
            Route::get('/{id}', [\App\Http\Controllers\System\Admin\Notification::class, 'show']);
            Route::post('/{id}/read', [\App\Http\Controllers\System\Admin\Notification::class, 'markAsRead']);
            Route::post('/batch-read', [\App\Http\Controllers\System\Admin\Notification::class, 'batchMarkAsRead']);
            Route::post('/read-all', [\App\Http\Controllers\System\Admin\Notification::class, 'markAllAsRead']);
            Route::delete('/{id}', [\App\Http\Controllers\System\Admin\Notification::class, 'destroy']);
            Route::post('/batch-delete', [\App\Http\Controllers\System\Admin\Notification::class, 'batchDelete']);
            Route::post('/clear-read', [\App\Http\Controllers\System\Admin\Notification::class, 'clearRead']);
            Route::get('/statistics', [\App\Http\Controllers\System\Admin\Notification::class, 'statistics']);
            Route::post('/send', [\App\Http\Controllers\System\Admin\Notification::class, 'send']);
            Route::post('/retry-unsent', [\App\Http\Controllers\System\Admin\Notification::class, 'retryUnsent']);
        });
    });

    // WebSocket 管理
    Route::prefix('websocket')->group(function () {
        Route::get('/online-count', [\App\Http\Controllers\System\WebSocket::class, 'getOnlineCount']);
        Route::get('/online-users', [\App\Http\Controllers\System\WebSocket::class, 'getOnlineUsers']);
        Route::post('/check-online', [\App\Http\Controllers\System\WebSocket::class, 'checkOnline']);
        Route::post('/send-to-user', [\App\Http\Controllers\System\WebSocket::class, 'sendToUser']);
        Route::post('/send-to-users', [\App\Http\Controllers\System\WebSocket::class, 'sendToUsers']);
        Route::post('/broadcast', [\App\Http\Controllers\System\WebSocket::class, 'broadcast']);
        Route::post('/send-to-channel', [\App\Http\Controllers\System\WebSocket::class, 'sendToChannel']);
        Route::post('/send-notification', [\App\Http\Controllers\System\WebSocket::class, 'sendNotification']);
        Route::post('/send-notification-to-users', [\App\Http\Controllers\System\WebSocket::class, 'sendNotificationToUsers']);
        Route::post('/push-data-update', [\App\Http\Controllers\System\WebSocket::class, 'pushDataUpdate']);
        Route::post('/push-data-update-channel', [\App\Http\Controllers\System\WebSocket::class, 'pushDataUpdateToChannel']);
        Route::post('/disconnect-user', [\App\Http\Controllers\System\WebSocket::class, 'disconnectUser']);
    });
});
