<?php

use App\Models\Draw;
use App\Models\Player;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use App\Services\Admin\AdminReportQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('platform lifetime totals aggregate all draws with daily profit口径', function (): void {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'plt-p1',
        'username' => 'plt_u1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $drawA = Draw::query()->create([
        'draw_no' => '20260501-001',
        'business_date' => '2026-05-01',
        'sequence_no' => 1,
        'status' => 'settled',
        'start_time' => now()->subDays(2),
        'close_time' => now()->subDays(2)->addHour(),
        'draw_time' => now()->subDays(2)->addHours(2),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 1,
        'settle_version' => 1,
        'is_reopened' => false,
    ]);

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

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

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

    TicketItem::query()->create([
        'ticket_no' => 'TK-PLT-1',
        'order_id' => $orderA->id,
        'player_id' => $player->id,
        'draw_id' => $drawA->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => null,
        'unit_bet_amount' => 10_000,
        'total_bet_amount' => 10_000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 10_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' => 3_000,
        'jackpot_win_amount' => 0,
        'settled_at' => now(),
    ]);

    $totals = app(AdminReportQueryService::class)->platformLifetimeTotals();

    expect($totals['total_bet_minor'])->toBe(15_000)
        ->and($totals['total_payout_minor'])->toBe(3_000)
        ->and($totals['approx_house_gross_minor'])->toBe(12_000)
        ->and($totals['draw_count'])->toBe(2)
        ->and($totals['business_day_count'])->toBe(2)
        ->and($totals['date_from'])->toBe('2026-05-01')
        ->and($totals['date_to'])->toBe('2026-05-02')
        ->and($totals['currency_code'])->toBe('NPR');
});
