<?php

use App\Models\Player;
use App\Services\Player\PlayerCreditService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

uses(RefreshDatabase::class);

test('credit hold rejects second hold when available credit exhausted', 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' => 'cl-race',
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'username' => 'clr1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

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

    $credit = app(PlayerCreditService::class);
    $credit->holdForBet($player, 800);

    expect(fn () => $credit->holdForBet($player, 300))
        ->toThrow(ValidationException::class);

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

test('credit preflight rejects hold when overdue bill exists', function (): void {
    $site = DB::table('admin_sites')->where('is_default', true)->first();
    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => (int) $site->id,
        'period_start' => now()->subWeek(),
        'period_end' => now()->subDay(),
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $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' => 'cl-od',
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'username' => 'clod1',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

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

    DB::table('settlement_bills')->insert([
        'settlement_period_id' => $periodId,
        'bill_type' => 'player',
        'owner_type' => 'player',
        'owner_id' => $player->id,
        'counterparty_type' => 'agent',
        'counterparty_id' => $player->agent_node_id,
        'gross_win_loss' => 1000,
        'rebate_amount' => 0,
        'adjustment_amount' => 0,
        'net_amount' => 1000,
        'paid_amount' => 0,
        'unpaid_amount' => 1000,
        'status' => 'overdue',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $credit = app(PlayerCreditService::class);

    expect(fn () => $credit->assertCreditPreflight($player, 100))
        ->toThrow(ValidationException::class);
});
