<?php

use App\Models\Player;
use App\Support\PlayerAuthSource;
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 can view own pending settlement bills summary', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $player = Player::query()->create([
        'site_code' => 'default_site',
        'site_player_id' => 'native:settlement-bills',
        'auth_source' => PlayerAuthSource::LOTTERY_NATIVE,
        'funding_mode' => PlayerFundingMode::CREDIT,
        'username' => 'settlement_bills_player',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->subWeek(),
        'period_end' => now(),
        'status' => 'closed',
        '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' => 1,
            'gross_win_loss' => -50000,
            'rebate_amount' => 0,
            'adjustment_amount' => 0,
            'net_amount' => -50000,
            'paid_amount' => 10000,
            'unpaid_amount' => 40000,
            'status' => 'partial_paid',
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'settlement_period_id' => $periodId,
            'bill_type' => 'player',
            'owner_type' => 'player',
            'owner_id' => $player->id,
            'counterparty_type' => 'agent',
            'counterparty_id' => 1,
            'gross_win_loss' => 12000,
            'rebate_amount' => 0,
            'adjustment_amount' => 0,
            'net_amount' => 12000,
            'paid_amount' => 0,
            'unpaid_amount' => 12000,
            'status' => 'confirmed',
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/settlement-bills')
        ->assertOk()
        ->assertJsonPath('data.summary.pending_receivable', 40000)
        ->assertJsonPath('data.summary.pending_payable', 12000)
        ->assertJsonPath('data.summary.net_pending', 28000)
        ->assertJsonPath('data.summary.pending_count', 2)
        ->assertJsonPath('data.items.0.direction', 'payable')
        ->assertJsonPath('data.items.1.direction', 'receivable');
});

test('credit player win above credit limit is shown as agent payable to player', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $player = Player::query()->create([
        'site_code' => 'default_site',
        'site_player_id' => 'native:settlement-win-above-limit',
        'auth_source' => PlayerAuthSource::LOTTERY_NATIVE,
        'funding_mode' => PlayerFundingMode::CREDIT,
        'username' => 'settlement_win_above_limit',
        '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(),
    ]);

    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->subWeek(),
        'period_end' => now(),
        'status' => 'closed',
        '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' => 1,
        'gross_win_loss' => -25000,
        'rebate_amount' => 0,
        'adjustment_amount' => 0,
        'net_amount' => -25000,
        'paid_amount' => 0,
        'unpaid_amount' => 25000,
        'status' => 'confirmed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/settlement-bills')
        ->assertOk()
        ->assertJsonPath('data.summary.pending_receivable', 25000)
        ->assertJsonPath('data.summary.pending_payable', 0)
        ->assertJsonPath('data.summary.net_pending', 25000)
        ->assertJsonPath('data.items.0.direction', 'receivable')
        ->assertJsonPath('data.items.0.unpaid_amount', 25000)
        ->assertJsonPath('data.items.0.net_amount', -25000);
});

test('wallet player settlement bills response is empty', function (): void {
    $player = Player::query()->create([
        'site_code' => 'default_site',
        'site_player_id' => 'wallet:settlement-bills',
        'auth_source' => PlayerAuthSource::MAIN_SITE_SSO,
        'funding_mode' => PlayerFundingMode::WALLET,
        'username' => 'wallet_settlement_bills',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/settlement-bills')
        ->assertOk()
        ->assertJsonPath('data.summary.pending_count', 0)
        ->assertJsonCount(0, 'data.items');
});
