<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

test('locked bill can receive adjustment bill', function (): void {
    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => (int) DB::table('admin_sites')->where('is_default', true)->value('id'),
        'period_start' => now()->subDay(),
        'period_end' => now()->addDay(),
        '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,
        'gross_win_loss' => 1000,
        'rebate_amount' => 50,
        'adjustment_amount' => 0,
        'net_amount' => 930,
        'paid_amount' => 0,
        'unpaid_amount' => 930,
        'status' => 'confirmed',
        'locked_at' => now(),
        'confirmed_at' => now(),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $newId = app(\App\Services\AgentSettlement\AgentSettlementBillAdjustmentService::class)
        ->createAdjustment($billId, -30, 'adjustment', 'correction', 0);

    $adjustment = DB::table('settlement_bills')->where('id', $newId)->first();
    expect($adjustment)->not->toBeNull();
    expect((string) $adjustment->bill_type)->toBe('adjustment');
    expect((int) $adjustment->reversed_bill_id)->toBe($billId);
    expect((int) $adjustment->net_amount)->toBe(-30);
});
