<?php

use App\Models\AgentProfile;
use App\Models\Draw;
use App\Models\Player;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use App\Lottery\DrawStatus;
use App\Services\Agent\AgentNodeService;
use App\Services\AgentSettlement\AgentGameSettlementRecorder;
use App\Services\AgentSettlement\AgentSettlementPeriodCloseService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

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

test('period close aggregates share ledger written by game settlement recorder', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');

    $super = \App\Models\AdminUser::query()->create([
        'username' => 'pipe_super',
        'name' => 'Super',
        'email' => null,
        'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    AgentProfile::query()->where('agent_node_id', $rootId)->update([
        'total_share_rate' => 100,
        'default_player_rebate' => 0.005,
        'rebate_limit' => 0.01,
    ]);

    $leaf = app(AgentNodeService::class)->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'PIPE-C',
        'name' => 'Pipe C',
        'username' => 'pipe_c',
        'total_share_rate' => 25,
        'credit_limit' => 100_000,
        'rebate_limit' => 0.01,
        'default_player_rebate' => 0.005,
    ]));

    $player = Player::query()->create([
        'site_code' => $siteCode,
        'agent_node_id' => $leaf->id,
        'site_player_id' => 'pipe-p1',
        'username' => 'pipeplayer',
        'nickname' => null,
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    DB::table('player_credit_accounts')->insert([
        'player_id' => $player->id,
        'credit_limit' => 50_000,
        'used_credit' => 0,
        'frozen_credit' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('player_rebate_profiles')->insert([
        'player_id' => $player->id,
        'game_type' => '*',
        'rebate_rate' => 0.005,
        'extra_rebate_rate' => 0,
        'inherit_from_agent' => true,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $betMinor = 10_000;
    $draw = Draw::query()->create([
        'draw_no' => 'PIPE-DRAW-1',
        'business_date' => now()->toDateString(),
        'sequence_no' => 88,
        'status' => DrawStatus::Open->value,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $order = TicketOrder::query()->create([
        'order_no' => 'ORD-PIPE-1',
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => $betMinor,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => $betMinor,
        'total_estimated_payout' => 0,
        'status' => 'confirmed',
        'submit_source' => 'h5',
        'client_trace_id' => 'pipe-trace-1',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $item = TicketItem::query()->create([
        'ticket_no' => 'T-PIPE-1',
        'order_id' => $order->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 2,
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => $betMinor,
        'total_bet_amount' => $betMinor,
        'rebate_rate_snapshot' => '0.0000',
        'commission_rate_snapshot' => '0.0000',
        'actual_deduct_amount' => $betMinor,
        'odds_snapshot_json' => null,
        'rule_snapshot_json' => null,
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled_lose',
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
    ]);
    $item->setRelation('player', $player);

    $recorder = app(AgentGameSettlementRecorder::class);
    expect($recorder->shouldRecord($item))->toBeTrue();

    $recorder->recordForTicketItem($item, 0, 'settled_lose');

    $item->refresh();
    expect($item->agent_settled_at)->not->toBeNull()
        ->and($item->share_snapshot)->not->toBeNull();

    $ledger = DB::table('share_ledger')->where('ticket_item_id', $item->id)->first();
    expect($ledger)->not->toBeNull()
        ->and((int) $ledger->game_win_loss)->toBe($betMinor);

    $settledAt = (string) $ledger->settled_at;
    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->parse($settledAt)->subDay(),
        'period_end' => now()->parse($settledAt)->addDay(),
        'status' => 'open',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $close = app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);

    expect($close['player_count'])->toBe(1)
        ->and($close['bill_ids'])->not->toBeEmpty();

    $playerBill = DB::table('settlement_bills')
        ->where('settlement_period_id', $periodId)
        ->where('bill_type', 'player')
        ->where('owner_id', $player->id)
        ->first();

    expect($playerBill)->not->toBeNull()
        ->and((int) $playerBill->gross_win_loss)->toBe($betMinor)
        ->and((int) $playerBill->rebate_amount)->toBeGreaterThan(0);
});

test('credit settlement records base rebate plus add-on rebate and keeps extra rebate separate', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');

    $super = \App\Models\AdminUser::query()->create([
        'username' => 'pipe_extra_super',
        'name' => 'Super',
        'email' => null,
        'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    AgentProfile::query()->where('agent_node_id', $rootId)->update([
        'total_share_rate' => 100,
        'default_player_rebate' => 0.005,
        'rebate_limit' => 0.01,
    ]);

    $leaf = app(AgentNodeService::class)->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'PIPE-X',
        'name' => 'Pipe X',
        'username' => 'pipe_x',
        'total_share_rate' => 25,
        'credit_limit' => 100_000,
        'rebate_limit' => 0.01,
        'default_player_rebate' => 0.005,
        'can_grant_extra_rebate' => true,
    ]));

    $player = Player::query()->create([
        'site_code' => $siteCode,
        'agent_node_id' => $leaf->id,
        'site_player_id' => 'pipe-x1',
        'username' => 'pipeextra',
        'nickname' => null,
        'auth_source' => 'lottery_native',
        'funding_mode' => 'credit',
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    DB::table('player_credit_accounts')->insert([
        'player_id' => $player->id,
        'credit_limit' => 50_000,
        'used_credit' => 0,
        'frozen_credit' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('player_rebate_profiles')->insert([
        'player_id' => $player->id,
        'game_type' => 'big',
        'rebate_rate' => 0.005,
        'extra_rebate_rate' => 0.002,
        'inherit_from_agent' => false,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $betMinor = 10_000;
    $draw = Draw::query()->create([
        'draw_no' => 'PIPE-DRAW-X',
        'business_date' => now()->toDateString(),
        'sequence_no' => 108,
        'status' => DrawStatus::Open->value,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $order = TicketOrder::query()->create([
        'order_no' => 'ORD-PIPE-X',
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => $betMinor,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => $betMinor,
        'total_estimated_payout' => 0,
        'status' => 'confirmed',
        'submit_source' => 'h5',
        'client_trace_id' => 'pipe-trace-x',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $item = TicketItem::query()->create([
        'ticket_no' => 'T-PIPE-X',
        'order_id' => $order->id,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => '1234',
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 2,
        'digit_slot' => null,
        'bet_mode' => 'single',
        'unit_bet_amount' => $betMinor,
        'total_bet_amount' => $betMinor,
        'rebate_rate_snapshot' => '0.0000',
        'commission_rate_snapshot' => '0.0000',
        'actual_deduct_amount' => $betMinor,
        'odds_snapshot_json' => [
            ['prize_scope' => 'first', 'rebate_rate' => '0.0100', 'commission_rate' => '0.0000', 'odds_value' => 250000],
        ],
        'rule_snapshot_json' => [
            'base_rebate_rate' => '0.0100',
            'player_addon_rebate_rate' => '0.0070',
            'rebate_inherited_from_agent' => false,
        ],
        'combination_count' => 1,
        'estimated_max_payout' => 0,
        'risk_locked_amount' => 0,
        'status' => 'settled_lose',
        'win_amount' => 0,
        'jackpot_win_amount' => 0,
    ]);
    $item->setRelation('player', $player);

    app(AgentGameSettlementRecorder::class)->recordForTicketItem($item, 0, 'settled_lose');

    $item->refresh();
    expect((float) $item->agent_rebate_rate_snapshot)->toBe(0.015);

    $basic = DB::table('rebate_records')
        ->where('ticket_item_id', $item->id)
        ->where('rebate_type', 'basic')
        ->first();
    $extra = DB::table('rebate_records')
        ->where('ticket_item_id', $item->id)
        ->where('rebate_type', 'extra')
        ->first();

    expect($basic)->not->toBeNull()
        ->and((float) $basic->rebate_rate)->toBe(0.015)
        ->and((int) $basic->rebate_amount)->toBe(150);
    expect($extra)->not->toBeNull()
        ->and((float) $extra->rebate_rate)->toBe(0.002)
        ->and((int) $extra->rebate_amount)->toBe(20);

    $ledger = DB::table('share_ledger')->where('ticket_item_id', $item->id)->first();
    expect($ledger)->not->toBeNull()
        ->and((int) $ledger->basic_rebate)->toBe(150);

    $settledAt = (string) $ledger->settled_at;
    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->parse($settledAt)->subDay(),
        'period_end' => now()->parse($settledAt)->addDay(),
        'status' => 'open',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);

    $playerBill = DB::table('settlement_bills')
        ->where('settlement_period_id', $periodId)
        ->where('bill_type', 'player')
        ->where('owner_id', $player->id)
        ->first();

    expect($playerBill)->not->toBeNull()
        ->and((int) $playerBill->rebate_amount)->toBe(170);
});
