<?php

use App\Models\Draw;
use App\Models\Player;
use App\Models\AdminUser;
use App\Models\TicketOrder;
use App\Services\Admin\AdminReportQueryService;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('resolve dashboard period lifetime returns associative bounds without error', function (): void {
    $service = app(AdminReportQueryService::class);

    $empty = $service->resolveDashboardPeriod('lifetime', null, null);
    expect($empty)->toHaveKeys(['date_from', 'date_to'])
        ->and($empty['date_from'])->toBeString()
        ->and($empty['date_to'])->toBeString();

    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'lt-p1',
        'username' => 'lt_u1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    Draw::query()->create([
        'draw_no' => '20260101-001',
        'business_date' => '2026-01-01',
        'sequence_no' => 1,
        'status' => 'settled',
        'start_time' => now()->subMonths(2),
        'close_time' => now()->subMonths(2),
        'draw_time' => now()->subMonths(2),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 1,
        'settle_version' => 1,
        'is_reopened' => false,
    ]);

    $drawLate = Draw::query()->create([
        'draw_no' => '20260520-001',
        'business_date' => '2026-05-20',
        'sequence_no' => 1,
        'status' => 'settled',
        'start_time' => now()->subDay(),
        'close_time' => now()->subDay(),
        'draw_time' => now()->subDay(),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 1,
        'settle_version' => 1,
        'is_reopened' => false,
    ]);

    TicketOrder::query()->create([
        'order_no' => 'ORD-LT-1',
        'player_id' => $player->id,
        'draw_id' => $drawLate->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => 1_000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 1_000,
        'total_estimated_payout' => 0,
        'status' => 'settled',
        'submit_source' => 'h5',
        'client_trace_id' => null,
    ]);

    $range = $service->resolveDashboardPeriod('lifetime', null, null);
    expect($range['date_from'])->toBe('2026-05-20')
        ->and($range['date_to'])->toBe('2026-05-20');
});

test('dashboard analytics lifetime period returns ok via http', function (): void {
    $this->artisan('lottery:admin-auth-sync')->assertExitCode(0);

    $admin = AdminUser::query()->create([
        'username' => 'lt_dash_admin',
        'name' => 'LT Dash',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);
    $token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/dashboard/analytics?period=lifetime&metric=overview')
        ->assertOk()
        ->assertJsonStructure([
            'data' => [
                'date_from',
                'date_to',
                'summary',
                'daily_series',
            ],
        ]);
});
