<?php

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

uses(RefreshDatabase::class);

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

function dashboardSiteOperatorToken(string $username, string $roleSlug): string
{
    PlatformSystemRoles::ensureAll();

    $user = AdminUser::query()->create([
        'username' => $username,
        'name' => ucfirst(str_replace('_', ' ', $username)),
        'email' => null,
        'password' => 'secret-strong',
        'status' => 0,
    ]);

    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $roleId = (int) DB::table('admin_roles')->where('slug', $roleSlug)->value('id');

    DB::table('admin_user_site_roles')->insert([
        'admin_user_id' => $user->id,
        'site_id' => $siteId,
        'role_id' => $roleId,
        'granted_at' => now(),
    ]);

    return $user->createToken('test', ['*'], now()->addDay())->plainTextToken;
}

test('site finance dashboard returns finance overview without site admin overview', function (): void {
    $token = dashboardSiteOperatorToken('dash_site_finance', SiteOperatorRoles::SLUG_SITE_FINANCE);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/dashboard')
        ->assertOk()
        ->assertJsonPath('data.site_overview', null)
        ->assertJsonPath('data.site_cs_overview', null)
        ->assertJsonPath('data.site_finance_overview.site_code', fn ($code) => is_string($code) && $code !== '')
        ->assertJsonStructure([
            'data' => [
                'site_finance_overview' => [
                    'wallet_player_count',
                    'credit_player_count',
                    'pending_confirm_bill_count',
                    'payable_bill_count',
                    'payable_unpaid_minor',
                    'abnormal_transfer_count',
                ],
            ],
        ]);
});

test('site cs dashboard returns cs overview without finance or admin overview', function (): void {
    $token = dashboardSiteOperatorToken('dash_site_cs', SiteOperatorRoles::SLUG_SITE_CS);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/dashboard')
        ->assertOk()
        ->assertJsonPath('data.site_overview', null)
        ->assertJsonPath('data.site_finance_overview', null)
        ->assertJsonPath('data.site_cs_overview.site_code', fn ($code) => is_string($code) && $code !== '')
        ->assertJsonStructure([
            'data' => [
                'site_cs_overview' => [
                    'player_count',
                    'ticket_order_count_today',
                    'active_player_count_today',
                ],
            ],
        ]);
});

test('site admin dashboard still returns site overview only', function (): void {
    $token = dashboardSiteOperatorToken('dash_site_admin', SiteOperatorRoles::SLUG_SITE_ADMIN);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/dashboard')
        ->assertOk()
        ->assertJsonPath('data.site_finance_overview', null)
        ->assertJsonPath('data.site_cs_overview', null)
        ->assertJsonPath('data.site_overview.site_code', fn ($code) => is_string($code) && $code !== '');
});
