<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\V1\E2E\E2ECaptchaPeekController;
use App\Http\Controllers\Api\V1\E2E\E2EPlayerStateController;
use App\Http\Controllers\Api\V1\E2E\E2EDrawController;
use App\Http\Controllers\Api\V1\E2E\E2EInspectController;
use App\Http\Controllers\Api\V1\E2E\E2EProvisionController;

/**
 * E2E 专用路由：仅在 LOTTERY_E2E=true 时由 E2EServiceProvider 挂载。
 *
 * 设计：
 *  - 完全不污染生产路由表（生产环境 LOTTERY_E2E 默认 false）
 *  - 用 /api/v1/_e2e/* 前缀，与正常业务路由完全独立
 *  - 中间件只挂 throttle:60,1（无 locale 协商，captcha SVG 不依赖）
 *
 * 端点分组：
 *   captcha    : peek/peek-admin（约定码提示，e2e 用 LOTTERY_E2E_BYPASS）
 *   player     : reset / set-balance / unlock / inspect
 *   draw       : close-now / reopen / inspect
 *   inspect    : credit-ledger / wallet-txns / ticket-items（只读探查）
 */
Route::prefix('api/v1/_e2e')
    ->middleware((bool) env('LOTTERY_E2E', false) ? [] : ['throttle:60,1'])
    ->group(function (): void {
        // captcha bypass 提示
        Route::post('captcha/peek', [E2ECaptchaPeekController::class, 'player'])
            ->name('e2e.captcha.peek.player');
        Route::post('captcha/peek-admin', [E2ECaptchaPeekController::class, 'admin'])
            ->name('e2e.captcha.peek.admin');

        // 玩家状态：reset 兼容老路径（与 player 控制器方法对应）
        Route::post('reset-player', [E2EPlayerStateController::class, 'reset'])
            ->name('e2e.reset.player');
        Route::post('player/reset', [E2EPlayerStateController::class, 'reset'])
            ->name('e2e.player.reset');
        Route::post('player/set-balance', [E2EPlayerStateController::class, 'setBalance'])
            ->name('e2e.player.set-balance');
        Route::post('player/unlock', [E2EPlayerStateController::class, 'unlock'])
            ->name('e2e.player.unlock');
        Route::get('player/inspect', [E2EPlayerStateController::class, 'inspect'])
            ->name('e2e.player.inspect');

        // 期号时间快进
        Route::post('draw/{drawNo}/close-now', [E2EDrawController::class, 'closeNow'])
            ->name('e2e.draw.close-now');
        Route::post('draw/{drawNo}/finish-cooldown', [E2EDrawController::class, 'finishCooldown'])
            ->name('e2e.draw.finish-cooldown');
        Route::post('draw/{drawNo}/reopen', [E2EDrawController::class, 'reopen'])
            ->name('e2e.draw.reopen');
        Route::get('draw/{drawNo}/inspect', [E2EDrawController::class, 'inspect'])
            ->name('e2e.draw.inspect');
        Route::post('draw/tick', [E2EDrawController::class, 'tick'])
            ->name('e2e.draw.tick');

        // 只读探查
        Route::get('inspect/credit-ledger', [E2EInspectController::class, 'creditLedger'])
            ->name('e2e.inspect.credit-ledger');
        Route::get('inspect/wallet-txns', [E2EInspectController::class, 'walletTxns'])
            ->name('e2e.inspect.wallet-txns');
        Route::get('inspect/ticket-items', [E2EInspectController::class, 'ticketItems'])
            ->name('e2e.inspect.ticket-items');

        // 信用盘 / SSO / 主站钱包 mock
        Route::post('credit-player/setup', [E2EProvisionController::class, 'setupCreditPlayer'])
            ->name('e2e.credit-player.setup');
        Route::post('site-operator/setup', [E2EProvisionController::class, 'setupSiteOperator'])
            ->name('e2e.site-operator.setup');
        Route::post('settlement/reset', [E2EProvisionController::class, 'resetSiteSettlement'])
            ->name('e2e.settlement.reset');
        Route::post('sso/mint-jwt', [E2EProvisionController::class, 'mintSsoJwt'])
            ->name('e2e.sso.mint-jwt');
        Route::post('site/wallet-api', [E2EProvisionController::class, 'configureWalletApi'])
            ->name('e2e.site.wallet-api');
        Route::post('site/wallet-api/reset', [E2EProvisionController::class, 'resetWalletApi'])
            ->name('e2e.site.wallet-api.reset');
    });
