<?php

use App\Models\Player;
use App\Models\WalletTxn;
use App\Lottery\ErrorCode;
use App\Models\PlayerWallet;
use Database\Seeders\CurrencySeeder;
use Illuminate\Support\Facades\Http;
use Database\Seeders\LotterySettingsSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    config(['lottery.main_site.wallet_api_url' => null]);
    $this->seed(CurrencySeeder::class);
    $this->seed(LotterySettingsSeeder::class);
});

test('wallet logs returns transfer rows and pagination', function () {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'wl1',
        'username' => null,
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->postJson('/api/v1/wallet/transfer-in', [
            'amount' => 300,
            'idempotent_key' => 'log-test-1',
        ])
        ->assertOk();

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/logs?page=1&size=10')
        ->assertOk()
        ->assertJsonPath('code', ErrorCode::Success->value)
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.type', 'transfer_in');

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

test('wallet logs filters bet_deduct as public bet type', function () {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'wl3',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $wallet = PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 1000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    WalletTxn::query()->create([
        'txn_no' => 'WX_bet_deduct_1',
        'player_id' => $player->id,
        'wallet_id' => $wallet->id,
        'biz_type' => 'bet_deduct',
        'biz_no' => 'TO_test_bet',
        'direction' => 2,
        'amount' => 28900,
        'balance_before' => 100000,
        'balance_after' => 71100,
        'status' => 'posted',
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/logs?type=bet')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.type', 'bet')
        ->assertJsonPath('data.items.0.biz_type', 'bet_deduct');
});

test('wallet logs bet filter excludes bet_reverse shown as reversal', function () {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'wl5',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $wallet = PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 1000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    WalletTxn::query()->create([
        'txn_no' => 'WX_bet_deduct_2',
        'player_id' => $player->id,
        'wallet_id' => $wallet->id,
        'biz_type' => 'bet_deduct',
        'biz_no' => 'TO_bet_only',
        'direction' => 2,
        'amount' => 1100,
        'balance_before' => 100000,
        'balance_after' => 98900,
        'status' => 'posted',
    ]);

    WalletTxn::query()->create([
        'txn_no' => 'WX_bet_reverse_1',
        'player_id' => $player->id,
        'wallet_id' => $wallet->id,
        'biz_type' => 'bet_reverse',
        'biz_no' => 'TO_bet_only',
        'direction' => 1,
        'amount' => 1100,
        'balance_before' => 98900,
        'balance_after' => 100000,
        'status' => 'posted',
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/logs?type=bet')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.type', 'bet')
        ->assertJsonPath('data.items.0.biz_type', 'bet_deduct');

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/logs?type=reversal')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.type', 'reversal')
        ->assertJsonPath('data.items.0.biz_type', 'bet_reverse');
});

test('wallet logs filters settle_payout as public prize type', function () {
    $player = Player::query()->create([
        'site_code' => 'main',
        'site_player_id' => 'wl4',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $wallet = PlayerWallet::query()->create([
        'player_id' => $player->id,
        'wallet_type' => 'lottery',
        'currency_code' => 'NPR',
        'balance' => 1000,
        'frozen_balance' => 0,
        'status' => 0,
        'version' => 0,
    ]);

    WalletTxn::query()->create([
        'txn_no' => 'WX_settle_payout_1',
        'player_id' => $player->id,
        'wallet_id' => $wallet->id,
        'biz_type' => 'settle_payout',
        'biz_no' => 'SB_test_prize',
        'direction' => 1,
        'amount' => 50000,
        'balance_before' => 100000,
        'balance_after' => 150000,
        'status' => 'posted',
    ]);

    $this->withHeader('Authorization', 'Bearer dev:'.$player->id)
        ->getJson('/api/v1/wallet/logs?type=prize')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.type', 'prize')
        ->assertJsonPath('data.items.0.biz_type', 'settle_payout');
});
