<?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\Hash;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

function mintCsFinanceAdminToken(): string
{
    $admin = AdminUser::query()->create([
        'username' => 'cs_finance_admin',
        'name' => 'CS Finance QA',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);

    return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
}

test('admin lists ticket items for a player', function (): void {
    $token = mintCsFinanceAdminToken();

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

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

    $order = TicketOrder::query()->create([
        'order_no' => 'ORD-CSF-1',
        'player_id' => $player->id,
        'draw_id' => $draw->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' => null,
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'TKCSF0001',
        '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' => 1000,
        'total_bet_amount' => 1000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 1000,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
        'settled_at' => now(),
    ]);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?per_page=10')
        ->assertOk()
        ->assertJsonPath('data.player_id', $player->id)
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.ticket_no', 'TKCSF0001')
        ->assertJsonPath('data.items.0.draw_no', '20260520-001');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?draw_no=20260520-001')
        ->assertOk()
        ->assertJsonPath('data.total', 1);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?draw_no=20991231-999')
        ->assertOk()
        ->assertJsonPath('data.total', 0);
});

test('admin player ticket items support status number and date range filters', function (): void {
    $token = mintCsFinanceAdminToken();

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

    $draw1 = Draw::query()->create([
        'draw_no' => '20260520-003',
        'business_date' => '2026-05-20',
        'sequence_no' => 3,
        '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,
    ]);

    $draw2 = Draw::query()->create([
        'draw_no' => '20260520-004',
        'business_date' => '2026-05-20',
        'sequence_no' => 4,
        '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,
    ]);

    $order1 = TicketOrder::query()->create([
        'order_no' => 'ORD-CSF-3',
        'player_id' => $player->id,
        'draw_id' => $draw1->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' => null,
    ]);

    $order2 = TicketOrder::query()->create([
        'order_no' => 'ORD-CSF-4',
        'player_id' => $player->id,
        'draw_id' => $draw2->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' => null,
    ]);

    TicketOrder::query()->whereKey($order1->id)->update([
        'created_at' => '2026-05-01 10:00:00',
        'updated_at' => '2026-05-01 10:00:00',
    ]);
    TicketOrder::query()->whereKey($order2->id)->update([
        'created_at' => '2026-05-10 10:00:00',
        'updated_at' => '2026-05-10 10:00:00',
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'TKCSF0003',
        'order_id' => $order1->id,
        'player_id' => $player->id,
        'draw_id' => $draw1->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => null,
        'unit_bet_amount' => 1000,
        'total_bet_amount' => 1000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 1000,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'failed',
        'fail_reason_code' => 'risk_sold_out',
        'fail_reason_text' => 'Sold out',
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
        'settled_at' => null,
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'TKCSF0004',
        'order_id' => $order2->id,
        'player_id' => $player->id,
        'draw_id' => $draw2->id,
        'original_number' => '4321',
        'normalized_number' => '4321',
        'play_code' => 'small',
        'dimension' => 4,
        'digit_slot' => null,
        'bet_mode' => null,
        'unit_bet_amount' => 2000,
        'total_bet_amount' => 2000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 2000,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled_win',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 5000,
        'jackpot_win_amount' => 1000,
        'settled_at' => now(),
    ]);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?status[]=settled_win')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.ticket_no', 'TKCSF0004');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?number=1234')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.ticket_no', 'TKCSF0003');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?start_date=2026-05-09&end_date=2026-05-11')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.ticket_no', 'TKCSF0004');
});

test('admin draw finance summary aggregates bet and payout', function (): void {
    $token = mintCsFinanceAdminToken();

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

    $draw = Draw::query()->create([
        'draw_no' => '20260520-002',
        'business_date' => '2026-05-20',
        'sequence_no' => 2,
        '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-CSF-2',
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => 5000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 5000,
        'total_estimated_payout' => 0,
        'status' => 'settled',
        'submit_source' => 'h5',
        'client_trace_id' => null,
    ]);

    TicketItem::query()->create([
        'ticket_no' => 'TKCSF0002',
        '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' => null,
        'unit_bet_amount' => 5000,
        'total_bet_amount' => 5000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 5000,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 2000,
        'jackpot_win_amount' => 500,
        'settled_at' => now(),
    ]);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/draws/'.$draw->id.'/finance-summary')
        ->assertOk()
        ->assertJsonPath('data.draw_no', '20260520-002')
        ->assertJsonPath('data.total_bet_minor', 5000)
        ->assertJsonPath('data.total_win_payout_minor', 2000)
        ->assertJsonPath('data.total_jackpot_win_minor', 500)
        ->assertJsonPath('data.total_payout_minor', 2500)
        ->assertJsonPath('data.approx_house_gross_minor', 2500);
});
