<?php

use App\Models\AdminUser;
use App\Models\Draw;
use App\Models\Player;
use App\Lottery\DrawStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

uses(RefreshDatabase::class);

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

test('settlement period open hints api resource is configured after migrations', function (): void {
    expect(
        DB::table('admin_api_resources')
            ->where('route_name', 'api.v1.admin.settlement-periods.open-hints')
            ->where('status', 1)
            ->exists(),
    )->toBeTrue();
});

test('settlement period open hints returns suggested range and calendar markers', 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');

    DB::table('settlement_periods')->insert([
        'admin_site_id' => $siteId,
        'period_start' => '2026-04-30 16:00:00',
        'period_end' => '2026-05-31 15:59:59',
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $unpaidPeriodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => '2026-03-31 16:00:00',
        'period_end' => '2026-04-03 15:59:59',
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('settlement_bills')->insert([
        'settlement_period_id' => $unpaidPeriodId,
        'bill_type' => 'agent',
        'owner_type' => 'agent',
        'owner_id' => $rootId,
        'counterparty_type' => 'platform',
        'counterparty_id' => 0,
        'net_amount' => 1000,
        'unpaid_amount' => 1000,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $player = Player::query()->create([
        'site_code' => $siteCode,
        'agent_node_id' => $rootId,
        'site_player_id' => 'hints-player',
        'username' => 'hints_player',
        'nickname' => null,
        'default_currency' => 'NPR',
        'status' => 0,
    ]);

    $draw = Draw::query()->create([
        'draw_no' => 'DRAW-HINTS',
        'business_date' => '2026-06-05',
        'sequence_no' => 1,
        'status' => DrawStatus::Open->value,
        'current_result_version' => 0,
        'settle_version' => 0,
        'is_reopened' => false,
    ]);

    $orderId = (int) DB::table('ticket_orders')->insertGetId([
        'order_no' => 'ORD-HINTS',
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'currency_code' => 'NPR',
        'total_bet_amount' => 10_000,
        'total_rebate_amount' => 0,
        'total_actual_deduct' => 10_000,
        'total_estimated_payout' => 0,
        'status' => 'confirmed',
        'submit_source' => 'h5',
        'client_trace_id' => null,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $itemId = (int) DB::table('ticket_items')->insertGetId([
        'ticket_no' => 'T-HINTS',
        'order_id' => $orderId,
        'player_id' => $player->id,
        'draw_id' => $draw->id,
        'original_number' => null,
        'normalized_number' => '1234',
        'play_code' => 'big',
        'dimension' => 2,
        'digit_slot' => null,
        'bet_mode' => null,
        'unit_bet_amount' => 10_000,
        'total_bet_amount' => 10_000,
        'rebate_rate_snapshot' => 0,
        'commission_rate_snapshot' => 0,
        'actual_deduct_amount' => 10_000,
        '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,
        'settled_at' => null,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $settledAt = '2026-06-05 12:00:00';
    DB::table('share_ledger')->insert([
        'ticket_item_id' => $itemId,
        'player_id' => $player->id,
        'agent_node_id' => $rootId,
        'agent_path' => json_encode([$rootId]),
        'share_snapshot' => json_encode(['total_shares' => [$siteCode => 100]]),
        'game_win_loss' => 1000,
        'basic_rebate' => 0,
        'shared_net_win_loss' => 1000,
        'allocations_json' => json_encode([]),
        'settled_at' => $settledAt,
        'settlement_period_id' => null,
        'created_at' => $settledAt,
        'updated_at' => $settledAt,
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'open_hints_admin',
        'name' => 'Hints',
        '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-periods/open-hints?admin_site_id='.$siteId)
        ->assertOk()
        ->assertJsonPath('data.suggested_start', '2026-06-01')
        ->assertJsonPath('data.suggested_end', '2026-06-05')
        ->assertJsonPath('data.pending_activity_dates.0', '2026-06-05')
        ->assertJsonFragment(['2026-05-01'])
        ->assertJsonFragment(['2026-04-01'])
        ->assertJsonFragment(['2026-04-02'])
        ->assertJsonFragment(['2026-04-03']);
});

test('settlement period open hints does not suggest range overlapping occupied periods', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');

    DB::table('settlement_periods')->insert([
        'admin_site_id' => $siteId,
        'period_start' => '2026-05-31 16:00:00',
        'period_end' => '2026-06-30 15:59:59',
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'open_hints_overlap_admin',
        'name' => 'Hints Overlap',
        '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-periods/open-hints?admin_site_id='.$siteId)
        ->assertOk()
        ->assertJsonPath('data.suggested_start', '')
        ->assertJsonPath('data.suggested_end', '')
        ->assertJsonFragment(['2026-06-01'])
        ->assertJsonFragment(['2026-06-30']);
});
