<?php

use App\Models\Player;
use App\Support\PlayerFundingMode;
use Database\Seeders\CurrencySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

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

test('credit player wallet balance returns minor units matching admin credit limit', 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' => 'credit-bal-1',
        'auth_source' => 'lottery_native',
        'funding_mode' => PlayerFundingMode::CREDIT,
        'username' => 'creditbal',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

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

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/balance?currency=NPR')
        ->assertOk()
        ->assertJsonPath('data.credit_line_mode', true)
        ->assertJsonPath('data.available_balance', 20000)
        ->assertJsonPath('data.credit_limit', 20000)
        ->assertJsonPath('data.available_balance_formatted', '200.00');
});
