<?php

use App\Models\Draw;
use App\Models\Player;
use App\Models\WalletTxn;
use App\Models\TicketItem;
use App\Lottery\DrawStatus;
use App\Models\TicketOrder;
use App\Models\PlayerWallet;
use App\Models\DrawResultItem;
use App\Models\DrawResultBatch;
use App\Models\SettlementBatch;
use App\Models\AdminUser;
use Illuminate\Support\Facades\Hash;
use Database\Seeders\CurrencySeeder;
use Database\Seeders\PlayTypeSeeder;
use App\Lottery\DrawResultBatchStatus;
use App\Services\Draw\DrawPrizeLayout;
use Database\Seeders\LotterySettingsSeeder;
use Database\Seeders\OperationalConfigV1Seeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Services\Settlement\SettlementOrchestrator;
use App\Services\Settlement\SettlementBatchWorkflowService;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->seed(CurrencySeeder::class);
    $this->seed(PlayTypeSeeder::class);
    $this->seed(OperationalConfigV1Seeder::class);
    $this->seed(LotterySettingsSeeder::class);
});

test('settlement pays big winner and marks ticket settled', function (): void {
    $uniq = bin2hex(random_bytes(4));
    $player = Player::query()->create([
        'site_code' => 'test',
        'site_player_id' => 'settle-p-'.$uniq,
        'username' => 'sp_'.$uniq,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 5_000_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => '20260511-900',
        'business_date' => '2026-05-11',
        'sequence_no' => 900,
        'status' => DrawStatus::Open->value,
        'start_time' => now()->subMinutes(2),
        'close_time' => now()->addMinutes(5),
        'draw_time' => now()->addMinutes(6),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/ticket/place', [
            'draw_id' => '20260511-900',
            'currency_code' => 'NPR',
            'client_trace_id' => 'settle-trace-1',
            'lines' => [
                ['number' => '1234', 'play_code' => 'big', 'amount' => 10_000],
            ],
        ])
        ->assertOk();

    $batch = DrawResultBatch::query()->create([
        'draw_id' => $draw->id,
        'result_version' => 1,
        'source_type' => 'rng',
        'rng_seed_hash' => 'test',
        'raw_seed_encrypted' => null,
        'status' => DrawResultBatchStatus::Published->value,
        'created_by' => null,
        'confirmed_by' => null,
        'confirmed_at' => now(),
    ]);

    foreach (DrawPrizeLayout::slots() as $slot) {
        $num = $slot['prize_type'] === 'first' ? '1234' : '5678';
        $suffix3 = substr($num, -3);
        $suffix2 = substr($num, -2);
        DrawResultItem::query()->create([
            'draw_id' => $draw->id,
            'result_batch_id' => $batch->id,
            'prize_type' => $slot['prize_type'],
            'prize_index' => $slot['prize_index'],
            'number_4d' => $num,
            'suffix_3d' => $suffix3,
            'suffix_2d' => $suffix2,
            'head_digit' => (int) substr($num, 0, 1),
            'tail_digit' => (int) substr($num, 3, 1),
        ]);
    }

    $draw->forceFill([
        'status' => DrawStatus::Settling->value,
        'current_result_version' => 1,
    ])->save();

    $ran = app(SettlementOrchestrator::class)->trySettleDraw($draw->fresh());
    expect($ran)->toBeTrue();

    $admin = AdminUser::query()->create([
        'username' => 'settlement_legacy_reviewer',
        'name' => 'Settlement Legacy Reviewer',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);
    $settlementBatch = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
    app(SettlementBatchWorkflowService::class)->approve($settlementBatch, $admin);
    app(SettlementBatchWorkflowService::class)->payout($settlementBatch->fresh());

    $draw->refresh();
    expect($draw->status)->toBe(DrawStatus::Settled->value);
    expect((int) $draw->settle_version)->toBe(1);

    $item = TicketItem::query()->where('draw_id', $draw->id)->firstOrFail();
    expect($item->status)->toBe('settled_win');
    expect((int) $item->win_amount)->toBe(250_000);

    $order = TicketOrder::query()->whereKey($item->order_id)->firstOrFail();
    expect($order->status)->toBe('settled');

    expect(SettlementBatch::query()->where('draw_id', $draw->id)->count())->toBe(1);

    $wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
    expect((int) $wallet->balance)->toBe(5_000_000 - (int) $item->actual_deduct_amount + 250_000);

    expect(WalletTxn::query()->where('biz_type', 'settle_payout')->count())->toBe(1);
});

test('admin settlement requires review before payout and can export report', function (): void {
    $uniq = bin2hex(random_bytes(4));
    $player = Player::query()->create([
        'site_code' => 'test',
        'site_player_id' => 'settle-review-p-'.$uniq,
        'username' => 'srp_'.$uniq,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 5_000_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => '20260511-901',
        'business_date' => '2026-05-11',
        'sequence_no' => 901,
        'status' => DrawStatus::Open->value,
        'start_time' => now()->subMinutes(2),
        'close_time' => now()->addMinutes(5),
        'draw_time' => now()->addMinutes(6),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/ticket/place', [
            'draw_id' => '20260511-901',
            'currency_code' => 'NPR',
            'client_trace_id' => 'settle-review-trace-1',
            'lines' => [
                ['number' => '1234', 'play_code' => 'big', 'amount' => 10_000],
            ],
        ])
        ->assertOk();

    $batch = DrawResultBatch::query()->create([
        'draw_id' => $draw->id,
        'result_version' => 1,
        'source_type' => 'rng',
        'rng_seed_hash' => 'review-test',
        'raw_seed_encrypted' => null,
        'status' => DrawResultBatchStatus::Published->value,
        'created_by' => null,
        'confirmed_by' => null,
        'confirmed_at' => now(),
    ]);

    foreach (DrawPrizeLayout::slots() as $slot) {
        $num = $slot['prize_type'] === 'first' ? '1234' : '5678';
        DrawResultItem::query()->create([
            'draw_id' => $draw->id,
            'result_batch_id' => $batch->id,
            'prize_type' => $slot['prize_type'],
            'prize_index' => $slot['prize_index'],
            'number_4d' => $num,
            'suffix_3d' => substr($num, -3),
            'suffix_2d' => substr($num, -2),
            'head_digit' => (int) substr($num, 0, 1),
            'tail_digit' => (int) substr($num, 3, 1),
        ]);
    }

    $draw->forceFill([
        'status' => DrawStatus::Settling->value,
        'current_result_version' => 1,
    ])->save();

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

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/draws/{$draw->id}/settlement/run")
        ->assertOk()
        ->assertJsonPath('data.status', DrawStatus::Settling->value);

    $settlement = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
    expect($settlement->status)->toBe('pending_review');

    $item = TicketItem::query()->where('draw_id', $draw->id)->firstOrFail();
    expect($item->status)->toBe('pending_payout');
    expect(WalletTxn::query()->where('biz_type', 'settle_payout')->count())->toBe(0);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/settlement-batches/{$settlement->id}/approve")
        ->assertOk()
        ->assertJsonPath('data.review_status', 'approved');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/settlement-batches/{$settlement->id}/payout")
        ->assertOk()
        ->assertJsonPath('data.status', 'paid');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-batches?draw_no=20260511-901&status=paid')
        ->assertOk()
        ->assertJsonPath('data.items.0.total_bet_amount', 10_000)
        ->assertJsonPath('data.items.0.total_actual_deduct', 10_000)
        ->assertJsonPath('data.items.0.total_payout_amount', 250_000)
        ->assertJsonPath('data.items.0.platform_profit', -240_000);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson("/api/v1/admin/settlement-batches/{$settlement->id}")
        ->assertOk()
        ->assertJsonPath('data.total_bet_amount', 10_000)
        ->assertJsonPath('data.total_actual_deduct', 10_000)
        ->assertJsonPath('data.platform_profit', -240_000);

    $item->refresh();
    expect($item->status)->toBe('settled_win');
    expect(WalletTxn::query()->where('biz_type', 'settle_payout')->count())->toBe(1);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->get("/api/v1/admin/settlement-batches/{$settlement->id}/export")
        ->assertOk()
        ->assertHeader('content-type', 'text/csv; charset=UTF-8');
});

test('settlement reject reverts tickets to pending_draw and allows re-settlement', function (): void {
    $uniq = bin2hex(random_bytes(4));
    $player = Player::query()->create([
        'site_code' => 'test',
        'site_player_id' => 'settle-reject-p-'.$uniq,
        'username' => 'srp_'.$uniq,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 5_000_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => '20260511-902',
        'business_date' => '2026-05-11',
        'sequence_no' => 902,
        'status' => DrawStatus::Open->value,
        'start_time' => now()->subMinutes(2),
        'close_time' => now()->addMinutes(5),
        'draw_time' => now()->addMinutes(6),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/ticket/place', [
            'draw_id' => '20260511-902',
            'currency_code' => 'NPR',
            'client_trace_id' => 'settle-reject-trace-1',
            'lines' => [
                ['number' => '1234', 'play_code' => 'big', 'amount' => 10_000],
            ],
        ])
        ->assertOk();

    $batch = DrawResultBatch::query()->create([
        'draw_id' => $draw->id,
        'result_version' => 1,
        'source_type' => 'rng',
        'rng_seed_hash' => 'reject-test',
        'raw_seed_encrypted' => null,
        'status' => DrawResultBatchStatus::Published->value,
        'created_by' => null,
        'confirmed_by' => null,
        'confirmed_at' => now(),
    ]);

    foreach (DrawPrizeLayout::slots() as $slot) {
        $num = $slot['prize_type'] === 'first' ? '1234' : '5678';
        DrawResultItem::query()->create([
            'draw_id' => $draw->id,
            'result_batch_id' => $batch->id,
            'prize_type' => $slot['prize_type'],
            'prize_index' => $slot['prize_index'],
            'number_4d' => $num,
            'suffix_3d' => substr($num, -3),
            'suffix_2d' => substr($num, -2),
            'head_digit' => (int) substr($num, 0, 1),
            'tail_digit' => (int) substr($num, 3, 1),
        ]);
    }

    $draw->forceFill([
        'status' => DrawStatus::Settling->value,
        'current_result_version' => 1,
    ])->save();

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

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/draws/{$draw->id}/settlement/run")
        ->assertOk();

    $settlement = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
    $item = TicketItem::query()->where('draw_id', $draw->id)->firstOrFail();
    expect($item->status)->toBe('pending_payout');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/settlement-batches/{$settlement->id}/reject", ['remark' => 'wrong result'])
        ->assertOk()
        ->assertJsonPath('data.review_status', 'rejected');

    $item->refresh();
    expect($item->status)->toBe('pending_draw')
        ->and((int) $item->win_amount)->toBe(0);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/draws/{$draw->id}/settlement/run")
        ->assertOk()
        ->assertJsonPath('data.ran', true);

    $second = SettlementBatch::query()
        ->where('draw_id', $draw->id)
        ->where('status', 'pending_review')
        ->orderByDesc('id')
        ->firstOrFail();

    expect((int) $second->id)->toBeGreaterThan((int) $settlement->id);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/settlement-batches/{$second->id}/approve")
        ->assertOk();

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson("/api/v1/admin/settlement-batches/{$second->id}/payout")
        ->assertOk();

    $item->refresh();
    $draw->refresh();
    expect($item->status)->toBe('settled_win')
        ->and($draw->status)->toBe(DrawStatus::Settled->value);
});

test('payout credits same player separately per currency', function (): void {
    $uniq = bin2hex(random_bytes(4));
    $player = Player::query()->create([
        'site_code' => 'test',
        'site_player_id' => 'settle-multi-currency-'.$uniq,
        'username' => 'smc_'.$uniq,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 100_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);
    PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'USD',
        'balance' => 200_000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => '20260511-990',
        'business_date' => '2026-05-11',
        'sequence_no' => 990,
        'status' => DrawStatus::Settling->value,
        'start_time' => now()->subMinutes(10),
        'close_time' => now()->subMinutes(5),
        'draw_time' => now()->subMinutes(4),
        'cooling_end_time' => null,
        'result_source' => null,
        'current_result_version' => 1,
        'settle_version' => 1,
        'is_reopened' => false,
    ]);
    $resultBatch = DrawResultBatch::query()->create([
        'draw_id' => $draw->id,
        'result_version' => 1,
        'source_type' => 'rng',
        'rng_seed_hash' => 'multi-currency',
        'raw_seed_encrypted' => null,
        'status' => DrawResultBatchStatus::Published->value,
        'created_by' => null,
        'confirmed_by' => null,
        'confirmed_at' => now(),
    ]);

    $orderNpr = TicketOrder::query()->create([
        'order_no' => 'TO-NPR-'.$uniq,
        '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' => 'placed',
        'submit_source' => 'h5',
        'client_trace_id' => null,
        'play_config_version_no' => 1,
        'odds_version_no' => 1,
        'risk_cap_version_no' => 1,
    ]);
    $orderUsd = TicketOrder::query()->create([
        'order_no' => 'TO-USD-'.$uniq,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'USD',
        'total_bet_amount' => 2000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 2000,
        'total_estimated_payout' => 0,
        'status' => 'placed',
        'submit_source' => 'h5',
        'client_trace_id' => null,
        'play_config_version_no' => 1,
        'odds_version_no' => 1,
        'risk_cap_version_no' => 1,
    ]);

    $itemNpr = TicketItem::query()->create([
        'ticket_no' => 'TK-NPR-'.$uniq,
        'order_id' => $orderNpr->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 'D4',
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => 1000,
        'total_bet_amount' => 1000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 1000,
        'odds_snapshot_json' => [],
        'rule_snapshot_json' => [],
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'pending_payout',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 5000,
        'jackpot_win_amount' => 0,
        'settled_at' => null,
    ]);
    $itemUsd = TicketItem::query()->create([
        'ticket_no' => 'TK-USD-'.$uniq,
        'order_id' => $orderUsd->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '5678',
        'normalized_number' => '5678',
        'play_code' => 'big',
        'dimension' => 'D4',
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => 2000,
        'total_bet_amount' => 2000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 2000,
        'odds_snapshot_json' => [],
        'rule_snapshot_json' => [],
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'pending_payout',
        'fail_reason_code' => null,
        'fail_reason_text' => null,
        'win_amount' => 8000,
        'jackpot_win_amount' => 0,
        'settled_at' => null,
    ]);

    $batch = SettlementBatch::query()->create([
        'draw_id' => $draw->id,
        'result_batch_id' => $resultBatch->id,
        'settle_version' => 1,
        'status' => 'approved',
        'review_status' => 'approved',
        'reviewed_by' => null,
        'reviewed_at' => now(),
        'review_remark' => null,
        'started_at' => now()->subMinute(),
        'finished_at' => now(),
    ]);
    $batch->details()->create([
        'ticket_item_id' => $itemNpr->id,
        'matched_prize_tier' => 'first',
        'win_amount' => 5000,
        'jackpot_allocation_amount' => 0,
        'match_detail_json' => [],
    ]);
    $batch->details()->create([
        'ticket_item_id' => $itemUsd->id,
        'matched_prize_tier' => 'first',
        'win_amount' => 8000,
        'jackpot_allocation_amount' => 0,
        'match_detail_json' => [],
    ]);

    app(SettlementBatchWorkflowService::class)->payout($batch->fresh());

    $walletNpr = PlayerWallet::query()
        ->where('player_id', $player->id)
        ->where('currency_code', 'NPR')
        ->firstOrFail();
    $walletUsd = PlayerWallet::query()
        ->where('player_id', $player->id)
        ->where('currency_code', 'USD')
        ->firstOrFail();

    expect((int) $walletNpr->balance)->toBe(105_000)
        ->and((int) $walletUsd->balance)->toBe(208_000);

    expect(WalletTxn::query()
        ->where('biz_type', 'settle_payout')
        ->where('player_id', $player->id)
        ->count())->toBe(2);
});
