<?php

use App\Models\AdminUser;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

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

test('enabled agent primary account can login after create', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
    $service = app(\App\Services\Agent\AgentNodeService::class);

    $super = AdminUser::query()->create([
        'username' => 'login_super',
        'name' => 'Login Super',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    $child = $service->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'login-agent',
        'name' => 'Login Agent',
        'username' => 'agent_login_user',
        'status' => 1,
    ]));

    $userId = (int) DB::table('admin_user_agents')
        ->where('agent_node_id', $child->id)
        ->where('is_primary', true)
        ->value('admin_user_id');

    expect((int) AdminUser::query()->find($userId)?->status)->toBe(0);

    $captchaKey = (string) Str::uuid();
    Cache::put(
        'admin_captcha:'.$captchaKey,
        hash_hmac('sha256', 'xwz2', (string) config('app.key')),
        now()->addSeconds(120),
    );

    $this->postJson('/api/v1/admin/auth/login', [
        'account' => 'agent_login_user',
        'password' => agentNodeTestPassword(),
        'captcha_key' => $captchaKey,
        'captcha_code' => 'xwz2',
    ])
        ->assertOk()
        ->assertJsonPath('code', 0);
});

test('updating enabled agent keeps admin user login status active', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
    $service = app(\App\Services\Agent\AgentNodeService::class);

    $super = AdminUser::query()->create([
        'username' => 'login_super2',
        'name' => 'Login Super',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    $child = $service->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'login-agent2',
        'name' => 'Login Agent 2',
        'username' => 'agent_login_user2',
        'status' => 1,
    ]));

    $service->update($child, ['name' => 'Login Agent 2 Updated', 'status' => 1]);

    $userId = (int) DB::table('admin_user_agents')
        ->where('agent_node_id', $child->id)
        ->where('is_primary', true)
        ->value('admin_user_id');

    expect((int) AdminUser::query()->find($userId)?->status)->toBe(0);
});
