<?php

use App\Http\Controllers\AdminCommandController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\ChatBotController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\FishingController;
use App\Http\Controllers\RoomController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

// 聊天室首页 (即登录/注册页面)
Route::get('/', function () {
    if (Auth::check()) {
        return redirect()->route('rooms.index');
    }

    // 获取可用房间列表供登录页选择（原版风格）
    $rooms = \App\Models\Room::orderBy('id')->get();

    return view('index', compact('rooms'));
})->name('home');

// 处理登录/自动注册请求
Route::post('/login', [AuthController::class, 'login'])->name('login.post');

// 处理退出登录
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// 聊天室系统内部路由 (需要鉴权)
Route::middleware(['chat.auth'])->group(function () {
    // ---- 第六阶段：大厅与房间管理 ----
    Route::get('/guide', fn () => view('rooms.guide'))->name('guide');
    Route::get('/rooms', [RoomController::class, 'index'])->name('rooms.index');
    Route::post('/rooms', [RoomController::class, 'store'])->name('rooms.store');
    Route::put('/rooms/{id}', [RoomController::class, 'update'])->name('rooms.update');
    Route::delete('/rooms/{id}', [RoomController::class, 'destroy'])->name('rooms.destroy');
    Route::post('/rooms/{id}/transfer', [RoomController::class, 'transfer'])->name('rooms.transfer');

    // ---- 第九阶段：外围矩阵 - 风云排行榜 ----
    Route::get('/leaderboard', [\App\Http\Controllers\LeaderboardController::class, 'index'])->name('leaderboard.index');

    // ---- 第十阶段：站内信与留言板系统 ----
    Route::get('/guestbook', [\App\Http\Controllers\GuestbookController::class, 'index'])->name('guestbook.index');
    Route::post('/guestbook', [\App\Http\Controllers\GuestbookController::class, 'store'])->middleware('throttle:10,1')->name('guestbook.store');
    Route::delete('/guestbook/{id}', [\App\Http\Controllers\GuestbookController::class, 'destroy'])->name('guestbook.destroy');

    // ---- 第七阶段：用户资料与特权管理 ----
    Route::get('/user/{username}', [UserController::class, 'show'])->name('user.show');
    Route::put('/user/profile', [UserController::class, 'updateProfile'])->name('user.update_profile');
    Route::post('/user/send-email-code', [\App\Http\Controllers\Api\VerificationController::class, 'sendEmailCode'])->name('user.send_email_code');
    Route::put('/user/password', [UserController::class, 'changePassword'])->name('user.update_password');
    Route::post('/user/{username}/kick', [UserController::class, 'kick'])->name('user.kick');
    Route::post('/user/{username}/mute', [UserController::class, 'mute'])->name('user.mute');
    Route::post('/user/{username}/ban', [UserController::class, 'ban'])->name('user.ban');
    Route::post('/user/{username}/banip', [UserController::class, 'banIp'])->name('user.banip');

    // ---- 第五阶段：具体房间内部聊天核心 ----
    // 进入具体房间界面的初始化
    Route::get('/room/{id}', [ChatController::class, 'init'])->name('chat.room');

    // 发送消息
    Route::post('/room/{id}/send', [ChatController::class, 'send'])->name('chat.send');

    // 挂机心跳存点 (限制每分钟最多调用 6 次防止挂机脚本滥用)
    Route::post('/room/{id}/heartbeat', [ChatController::class, 'heartbeat'])
        ->middleware('throttle:6,1')
        ->name('chat.heartbeat');

    // 退出房间
    Route::post('/room/{id}/leave', [ChatController::class, 'leave'])->name('chat.leave');

    // 头像列表（供选择）
    Route::get('/headface/list', [ChatController::class, 'headfaceList'])->name('headface.list');

    // 修改头像
    Route::post('/headface/change', [ChatController::class, 'changeAvatar'])->name('headface.change');

    // 设置房间公告/祝福语
    Route::post('/room/{id}/announcement', [ChatController::class, 'setAnnouncement'])->name('chat.announcement');

    // ---- 钓鱼小游戏（复刻原版 diaoyu/ 功能）----
    Route::post('/room/{id}/fish/cast', [FishingController::class, 'cast'])->name('fishing.cast');
    Route::post('/room/{id}/fish/reel', [FishingController::class, 'reel'])->name('fishing.reel');

    // ---- AI 聊天机器人 ----
    Route::post('/chatbot/chat', [ChatBotController::class, 'chat'])->name('chatbot.chat');
    Route::post('/chatbot/clear', [ChatBotController::class, 'clearContext'])->name('chatbot.clear');

    // ---- 送花/礼物互动 ----
    Route::post('/gift/flower', [ChatController::class, 'sendFlower'])->name('gift.flower');

    // ---- 管理员命令（聊天室内实时操作）----
    Route::post('/command/warn', [AdminCommandController::class, 'warn'])->name('command.warn');
    Route::post('/command/kick', [AdminCommandController::class, 'kick'])->name('command.kick');
    Route::post('/command/mute', [AdminCommandController::class, 'mute'])->name('command.mute');
    Route::post('/command/freeze', [AdminCommandController::class, 'freeze'])->name('command.freeze');
    Route::get('/command/whispers/{username}', [AdminCommandController::class, 'viewWhispers'])->name('command.whispers');
    Route::post('/command/announce', [AdminCommandController::class, 'announce'])->name('command.announce');
    Route::post('/command/clear-screen', [AdminCommandController::class, 'clearScreen'])->name('command.clear_screen');
    Route::post('/command/effect', [AdminCommandController::class, 'effect'])->name('command.effect');

    // ---- 商店（购买特效卡/改名卡）----
    Route::get('/shop/items', [\App\Http\Controllers\ShopController::class, 'items'])->name('shop.items');
    Route::post('/shop/buy', [\App\Http\Controllers\ShopController::class, 'buy'])->name('shop.buy');
    Route::post('/shop/rename', [\App\Http\Controllers\ShopController::class, 'rename'])->name('shop.rename');
});

// 强力特权层中间件：同时验证 chat.auth 登录态 和 chat.level:super 特权（superlevel 由 sysparam 配置）
Route::middleware(['chat.auth', 'chat.level:super'])->prefix('admin')->name('admin.')->group(function () {
    // 后台首页概览
    Route::get('/', [\App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('dashboard');

    // 系统参数配置 (替代 VIEWSYS.ASP / SetSYS.ASP)
    Route::middleware(['chat.site_owner'])->group(function () {
        Route::get('/system', [\App\Http\Controllers\Admin\SystemController::class, 'edit'])->name('system.edit');
        Route::put('/system', [\App\Http\Controllers\Admin\SystemController::class, 'update'])->name('system.update');

        // 发信配置管理
        Route::get('/smtp', [\App\Http\Controllers\Admin\SmtpController::class, 'edit'])->name('smtp.edit');
        Route::put('/smtp', [\App\Http\Controllers\Admin\SmtpController::class, 'update'])->name('smtp.update');
        Route::post('/smtp/test', [\App\Http\Controllers\Admin\SmtpController::class, 'test'])->name('smtp.test');
    });

    // 用户大盘管理 (替代 gl/ 目录下的各种用户管理功能)
    Route::get('/users', [\App\Http\Controllers\Admin\UserManagerController::class, 'index'])->name('users.index');
    Route::put('/users/{id}', [\App\Http\Controllers\Admin\UserManagerController::class, 'update'])->name('users.update');
    Route::delete('/users/{id}', [\App\Http\Controllers\Admin\UserManagerController::class, 'destroy'])->name('users.destroy'); // 物理封杀

    // 房间管理
    Route::get('/rooms', [\App\Http\Controllers\Admin\RoomManagerController::class, 'index'])->name('rooms.index');
    Route::put('/rooms/{id}', [\App\Http\Controllers\Admin\RoomManagerController::class, 'update'])->name('rooms.update');
    Route::delete('/rooms/{id}', [\App\Http\Controllers\Admin\RoomManagerController::class, 'destroy'])->name('rooms.destroy');

    // 随机事件管理（复刻原版 autoact 系统）
    Route::get('/autoact', [\App\Http\Controllers\Admin\AutoactController::class, 'index'])->name('autoact.index');
    Route::post('/autoact', [\App\Http\Controllers\Admin\AutoactController::class, 'store'])->name('autoact.store');
    Route::put('/autoact/{id}', [\App\Http\Controllers\Admin\AutoactController::class, 'update'])->name('autoact.update');
    Route::post('/autoact/{id}/toggle', [\App\Http\Controllers\Admin\AutoactController::class, 'toggle'])->name('autoact.toggle');
    Route::delete('/autoact/{id}', [\App\Http\Controllers\Admin\AutoactController::class, 'destroy'])->name('autoact.destroy');

    // VIP 会员等级管理
    Route::get('/vip', [\App\Http\Controllers\Admin\VipController::class, 'index'])->name('vip.index');
    Route::post('/vip', [\App\Http\Controllers\Admin\VipController::class, 'store'])->name('vip.store');
    Route::put('/vip/{id}', [\App\Http\Controllers\Admin\VipController::class, 'update'])->name('vip.update');
    Route::delete('/vip/{id}', [\App\Http\Controllers\Admin\VipController::class, 'destroy'])->name('vip.destroy');

    // AI 厂商配置管理
    Route::middleware(['chat.site_owner'])->group(function () {
        Route::get('/ai-providers', [\App\Http\Controllers\Admin\AiProviderController::class, 'index'])->name('ai-providers.index');
        Route::post('/ai-providers', [\App\Http\Controllers\Admin\AiProviderController::class, 'store'])->name('ai-providers.store');
        Route::put('/ai-providers/{id}', [\App\Http\Controllers\Admin\AiProviderController::class, 'update'])->name('ai-providers.update');
        Route::post('/ai-providers/{id}/toggle', [\App\Http\Controllers\Admin\AiProviderController::class, 'toggleEnabled'])->name('ai-providers.toggle');
        Route::post('/ai-providers/{id}/default', [\App\Http\Controllers\Admin\AiProviderController::class, 'setDefault'])->name('ai-providers.default');
        Route::post('/ai-providers/toggle-chatbot', [\App\Http\Controllers\Admin\AiProviderController::class, 'toggleChatBot'])->name('ai-providers.toggle-chatbot');
        Route::delete('/ai-providers/{id}', [\App\Http\Controllers\Admin\AiProviderController::class, 'destroy'])->name('ai-providers.destroy');
    });
});
