<?php

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

uses(RefreshDatabase::class);

test('payment record uses counterparty as payer when player wins', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $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' => 1,
        'counterparty_type' => 'agent',
        'counterparty_id' => 1,
        'net_amount' => -500,
        'unpaid_amount' => 500,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'pay_dir_super',
        'name' => 'PayDir',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);

    app(SettlementPaymentService::class)->recordPayment($billId, 500, (int) $admin->id, [
        'method' => 'cash',
    ]);

    $record = DB::table('payment_records')->where('settlement_bill_id', $billId)->first();
    expect($record)->not->toBeNull();
    expect((string) $record->payer_type)->toBe('agent');
    expect((int) $record->payer_id)->toBe(1);
    expect((string) $record->payee_type)->toBe('player');
    expect((int) $record->payee_id)->toBe(1);
});

test('payment record uses owner as payer when player loses', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $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' => 1,
        'counterparty_type' => 'agent',
        'counterparty_id' => 1,
        'net_amount' => 800,
        'unpaid_amount' => 800,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'pay_dir_super2',
        'name' => 'PayDir2',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);

    app(SettlementPaymentService::class)->recordPayment($billId, 800, (int) $admin->id);

    $record = DB::table('payment_records')->where('settlement_bill_id', $billId)->first();
    expect((string) $record->payer_type)->toBe('player');
    expect((int) $record->payer_id)->toBe(1);
    expect((string) $record->payee_type)->toBe('agent');
    expect((int) $record->payee_id)->toBe(1);
});
