<?php

namespace Tests\Feature\Bo;

use App\Models\KycDocument;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class KycModerationTest extends TestCase
{
    use RefreshDatabase;

    public function test_guest_is_redirected_to_login_alias_for_bo_dashboard(): void
    {
        $this->get(route('bo.dashboard'))
            ->assertRedirect(route('login'));
    }

    public function test_admin_can_approve_kyc_document_from_bo(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $liner = User::factory()->create([
            'role' => 'liner',
            'is_kyc_verified' => false,
        ]);

        $doc = KycDocument::create([
            'user_id' => $liner->id,
            'document_type' => 'passport',
            'document_number' => 'TEST-123',
            'status' => 'pending',
        ]);

        $this->actingAs($admin)
            ->post(route('bo.kyc.approve', $doc))
            ->assertRedirect();

        $this->assertDatabaseHas('kyc_documents', [
            'id' => $doc->id,
            'status' => 'approved',
            'validated_by' => $admin->id,
        ]);

        $this->assertDatabaseHas('users', [
            'id' => $liner->id,
            'is_kyc_verified' => 1,
        ]);
    }

    public function test_non_admin_cannot_moderate_kyc_document(): void
    {
        $client = User::factory()->create([
            'role' => 'client',
        ]);

        $liner = User::factory()->create([
            'role' => 'liner',
        ]);

        $doc = KycDocument::create([
            'user_id' => $liner->id,
            'document_type' => 'id_card',
            'document_number' => 'TEST-999',
            'status' => 'pending',
        ]);

        $this->actingAs($client)
            ->post(route('bo.kyc.approve', $doc))
            ->assertForbidden();
    }
}
