<?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('resolve date range defaults to last 30 days when filters empty', function (): void {
    $range = app(AdminReportQueryService::class)->resolveDateRange([]);

    expect($range['date_to'])->toBe(now()->toDateString())
        ->and($range['date_from'])->toBe(now()->subDays(29)->toDateString());
});

test('daily profit rows omit business days with no betting activity', function (): void {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'rpt-daily-1',
        'username' => 'daily_rpt',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    Draw::query()->create([
        'draw_no' => '20260502-001',
        'business_date' => '2026-05-02',
        'sequence_no' => 2,
        '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,
    ]);

    $activeDraw = 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,
    ]);

    $order = TicketOrder::query()->create([
        'order_no' => 'RPT-DAILY-ORD',
        'player_id' => $player->id,
        'draw_id' => $activeDraw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => 1000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 1000,
        'total_estimated_payout' => 0,
        'status' => 'settled',
        'submit_source' => 'h5',
        'client_trace_id' => 'trace-daily',
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'RPT-DAILY-TK',
        'order_id' => $order->id,
        'player_id' => $player->id,
        'draw_id' => $activeDraw->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => 1000,
        'total_bet_amount' => 1000,
        'rebate_rate_snapshot' => '0.0000',
        'commission_rate_snapshot' => '0.0000',
        'actual_deduct_amount' => 1000,
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
        'status' => 'settled',
    ]);

    $rows = app(AdminReportQueryService::class)->dailyProfitRows('2026-05-01', '2026-05-02');

    expect($rows)->toHaveCount(1)
        ->and($rows[0]['business_date'])->toBe('2026-05-01')
        ->and($rows[0]['total_bet_minor'])->toBe(1000);
});

test('player win loss uses draw business date not order created date', function (): void {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'rpt-axis-1',
        'username' => 'axis_user',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $businessDate = now()->toDateString();
    $orderCreatedAt = now()->addDay()->startOfDay();

    $draw = Draw::query()->create([
        'draw_no' => 'RPT-AXIS-'.now()->format('YmdHis'),
        'business_date' => $businessDate,
        'sequence_no' => 1,
        'status' => 'settled',
        'start_time' => now()->subHour(),
        'close_time' => now()->addHour(),
        'draw_time' => now()->addHours(2),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 1,
        'settle_version' => 1,
        'is_reopened' => false,
    ]);

    $order = TicketOrder::query()->create([
        'order_no' => 'RPT-AXIS-ORD',
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => 2000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 2000,
        'total_estimated_payout' => 0,
        'status' => 'settled',
        'submit_source' => 'h5',
        'client_trace_id' => 'trace-axis',
        'created_at' => $orderCreatedAt,
        'updated_at' => $orderCreatedAt,
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'RPT-AXIS-TK',
        'order_id' => $order->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '5678',
        'normalized_number' => '5678',
        'play_code' => 'big',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => 2000,
        'total_bet_amount' => 2000,
        'rebate_rate_snapshot' => '0.0000',
        'commission_rate_snapshot' => '0.0000',
        'actual_deduct_amount' => 2000,
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
        'status' => 'settled',
    ]);

    $service = app(AdminReportQueryService::class);
    $orderDate = $orderCreatedAt->toDateString();

    expect($service->playerWinLossPaginated(null, $businessDate, $businessDate, 1, 20)->total())->toBe(1)
        ->and($service->playerWinLossPaginated(null, $orderDate, $orderDate, 1, 20)->total())->toBe(0);
});
