<?php

/**
 * PRD §17.2 可自动化子集（不含 k6 与万级结算压测）。
 */

use App\Models\Draw;
use App\Models\Player;
use App\Lottery\ErrorCode;
use App\Lottery\DrawStatus;
use App\Models\PlayerWallet;
use Carbon\Carbon;
use Database\Seeders\CurrencySeeder;
use Database\Seeders\PlayTypeSeeder;
use Database\Seeders\LotterySettingsSeeder;
use Database\Seeders\OperationalConfigV1Seeder;
use App\Services\Draw\DrawPlannerService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->seed(CurrencySeeder::class);
    $this->seed(PlayTypeSeeder::class);
    $this->seed(OperationalConfigV1Seeder::class);
    $this->seed(LotterySettingsSeeder::class);
});

test('draw planner schedules five minute draw_time gaps', function (): void {
    config([
        'lottery.draw.timezone' => 'UTC',
        'lottery.draw.interval_minutes' => 5,
        'lottery.draw.buffer_draws_ahead' => 12,
    ]);

    $fixed = Carbon::parse('2026-05-25 00:00:00', 'UTC');
    $report = app(DrawPlannerService::class)->ensureBuffer($fixed);
    expect($report['created'])->toBe(12);

    $times = Draw::query()
        ->whereNotNull('draw_time')
        ->orderBy('draw_time')
        ->limit(13)
        ->pluck('draw_time')
        ->map(fn ($t) => Carbon::parse($t)->utc())
        ->all();

    expect(count($times))->toBeGreaterThanOrEqual(2);

    for ($i = 1; $i < count($times); $i++) {
        $delta = (int) $times[$i]->diffInSeconds($times[$i - 1], absolute: true);
        expect($delta)->toBe(300);
    }
});

test('ticket place rejects bet when draw is closing', function (): void {
    $player = perfPlayer();
    Draw::query()->create([
        'draw_no' => '20260525-001',
        'business_date' => '2026-05-25',
        'sequence_no' => 1,
        'status' => DrawStatus::Closing->value,
        'start_time' => now()->subMinutes(10),
        'close_time' => now()->subMinute(),
        'draw_time' => now()->addMinute(),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/ticket/place', [
            'draw_id' => '20260525-001',
            'currency_code' => 'NPR',
            'client_trace_id' => 'perf-sealed',
            'lines' => [['number' => '1234', 'play_code' => 'big', 'amount' => 10]],
        ])
        ->assertStatus(400)
        ->assertJsonPath('code', ErrorCode::DrawClosed->value);
});

test('ticket place rejects bet when close_time passed but status still open', function (): void {
    $player = perfPlayer();
    Draw::query()->create([
        'draw_no' => '20260525-002',
        'business_date' => '2026-05-25',
        'sequence_no' => 2,
        'status' => DrawStatus::Open->value,
        'start_time' => now()->subMinutes(10),
        'close_time' => now()->subSecond(),
        'draw_time' => now()->addMinute(),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/ticket/place', [
            'draw_id' => '20260525-002',
            'currency_code' => 'NPR',
            'client_trace_id' => 'perf-close-time',
            'lines' => [['number' => '5678', 'play_code' => 'big', 'amount' => 10]],
        ])
        ->assertStatus(400)
        ->assertJsonPath('code', ErrorCode::DrawClosed->value);
});

function perfPlayer(): Player
{
    $uniq = bin2hex(random_bytes(4));
    $player = Player::query()->create([
        'site_code' => 'test',
        'site_player_id' => 'perf-'.$uniq,
        'username' => 'perf_'.$uniq,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 1_000_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    return $player;
}
