<?php

use App\Models\Draw;
use App\Models\Player;
use App\Models\AdminUser;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('dashboard analytics returns summary trend and play breakdown for period', function (): void {
    $this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
    $siteCode = (string) DB::table('admin_sites')->where('is_default', true)->value('code');
    $businessDate = now()->subDays(3)->toDateString();
    $player = Player::query()->create([
        'site_code' => $siteCode !== '' ? $siteCode : 'default_site',
        'site_player_id' => 'da-p1',
        'username' => 'da_u1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => '20260510-001',
        'business_date' => $businessDate,
        '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,
    ]);

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

    TicketItem::query()->create([
        'ticket_no' => 'TK-DA-1',
        'order_id' => $order->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => null,
        'unit_bet_amount' => 8_000,
        'total_bet_amount' => 8_000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 8_000,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'won',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 2_000,
        'jackpot_win_amount' => 0,
        'settled_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'dash_analytics_admin',
        'name' => 'Dash Analytics QA',
        '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=last_30_days&metric=overview')
        ->assertOk()
        ->assertJsonPath('data.summary.total_bet_minor', 8_000)
        ->assertJsonPath('data.summary.total_payout_minor', 2_000)
        ->assertJsonPath('data.summary.approx_house_gross_minor', 6_000)
        ->assertJsonStructure([
            'data' => [
                'daily_series',
                'play_breakdown',
                'chart_meta' => ['truncated', 'span_days'],
            ],
        ]);
});
