<?php

use App\Models\Player;
use App\Services\AgentSettlement\AgentGameSettlementRecorder;
use App\Services\Player\PlayerCreditService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

test('settled loss does not double count bet hold', function (): void {
    $site = DB::table('admin_sites')->where('is_default', true)->first();
    $player = Player::query()->create([
        'site_code' => (string) $site->code,
        'agent_node_id' => (int) DB::table('agent_nodes')->where('depth', 0)->value('id'),
        'site_player_id' => 'hold-p1',
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'username' => 'hold1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    DB::table('player_credit_accounts')->insert([
        'player_id' => $player->id,
        'credit_limit' => 5000,
        'used_credit' => 0,
        'frozen_credit' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $credit = app(PlayerCreditService::class);
    $credit->assertMayPlaceBet($player, 200);
    expect((int) DB::table('player_credit_accounts')->where('player_id', $player->id)->value('used_credit'))->toBe(2);

    $drawId = (int) \App\Models\Draw::query()->create([
        'draw_no' => 'HOLD-DRAW',
        'business_date' => now()->toDateString(),
        'sequence_no' => 1,
        'status' => \App\Lottery\DrawStatus::Open->value,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ])->id;

    $orderId = (int) DB::table('ticket_orders')->insertGetId([
        'order_no' => 'ORD-HOLD-1',
        'player_id' => $player->id,
        'draw_id' => $drawId,
        'currency_code' => 'NPR',
        'total_bet_amount' => 200,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 200,
        'total_estimated_payout' => 0,
        'status' => 'placed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $item = \App\Models\TicketItem::query()->create([
        'ticket_no' => 'T-HOLD-1',
        'order_id' => $orderId,
        'player_id' => $player->id,
        'draw_id' => $drawId,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'direct',
        'dimension' => '4d',
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => 200,
        'total_bet_amount' => 200,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 200,
        'odds_snapshot_json' => '{}',
        'rule_snapshot_json' => '{}',
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled_lose',
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
    ]);
    $item->setRelation('player', $player);

    app(AgentGameSettlementRecorder::class)->recordForTicketItem($item, 0, 'settled_lose');

    expect((int) DB::table('player_credit_accounts')->where('player_id', $player->id)->value('used_credit'))->toBe(2);
});
