<?php

use App\Models\AdminUser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});

test('settlement payments and adjustments index return items', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
    $playerId = (int) DB::table('players')->insertGetId([
        'site_code' => (string) DB::table('admin_sites')->where('id', $siteId)->value('code'),
        'site_player_id' => 'lists-player',
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'username' => 'lists_player',
        'default_currency' => 'NPR',
        'status' => 0,
        'agent_node_id' => $rootId,
        '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(),
    ]);

    $billId = (int) DB::table('settlement_bills')->insertGetId([
        'settlement_period_id' => $periodId,
        'bill_type' => 'player',
        'owner_type' => 'player',
        'owner_id' => $playerId,
        'counterparty_type' => 'agent',
        'counterparty_id' => $rootId,
        'net_amount' => 1000,
        'unpaid_amount' => 0,
        'paid_amount' => 1000,
        'status' => 'settled',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('payment_records')->insert([
        'settlement_bill_id' => $billId,
        'payer_type' => 'player',
        'payer_id' => $playerId,
        'payee_type' => 'agent',
        'payee_id' => $rootId,
        'amount' => 1000,
        'method' => 'cash',
        'status' => 'confirmed',
        'confirmed_at' => now(),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('settlement_adjustments')->insert([
        'settlement_period_id' => $periodId,
        'original_bill_id' => $billId,
        'adjustment_type' => 'adjustment',
        'amount' => 100,
        'reason' => 'test',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'lists_super',
        'name' => 'Lists',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);
    $token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-payments?admin_site_id='.$siteId)
        ->assertOk()
        ->assertJsonPath('data.items.0.settlement_bill_id', $billId)
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.page', 1);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-adjustments?admin_site_id='.$siteId)
        ->assertOk()
        ->assertJsonPath('data.items.0.original_bill_id', $billId)
        ->assertJsonPath('data.total', 1);

    $operations = $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-operations?admin_site_id='.$siteId.'&settlement_period_id='.$periodId)
        ->assertOk()
        ->assertJsonPath('data.total', 2)
        ->json('data.items');

    expect(collect($operations)->pluck('kind')->sort()->values()->all())
        ->toBe(['adjustment', 'payment']);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-operations?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&operation_type=payment')
        ->assertOk()
        ->assertJsonPath('data.total', 1)
        ->assertJsonPath('data.items.0.kind', 'payment');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-bills?admin_site_id='.$siteId.'&bill_type=player')
        ->assertOk()
        ->assertJsonPath('data.items.0.bill_type', 'player');
});
