<?php

namespace Tests\Feature;

use App\Models\Participant;
use App\Models\User;
use App\Services\IcNumberService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CertificateSearchTest extends TestCase
{
    use RefreshDatabase;

    private string $testIc = '900101101234';

    protected function setUp(): void
    {
        parent::setUp();

        $icService = app(IcNumberService::class);

        Participant::create([
            'full_name' => 'AHMAD BIN ABDULLAH',
            'ic_number' => $icService->encrypt($this->testIc),
            'ic_number_hash' => $icService->hash($this->testIc),
            'participant_number' => 'A001',
            'category' => '5KM',
            'imported_by' => User::factory()->create()->id,
            'is_active' => true,
        ]);
    }

    public function test_public_page_is_accessible(): void
    {
        $this->get('/e-sijil')->assertOk();
    }

    public function test_search_with_full_name_returns_participant(): void
    {
        $response = $this->post('/e-sijil/search', [
            'name' => 'AHMAD BIN ABDULLAH',
        ]);

        $response->assertOk();
        $response->assertSee('AHMAD BIN ABDULLAH');
        $response->assertSee('Muat Turun e-Sijil');
        $response->assertSessionHas('verified_participant_ids');
    }

    public function test_partial_name_returns_matching_participant(): void
    {
        $response = $this->post('/e-sijil/search', [
            'name' => 'Ahmad',
        ]);

        $response->assertOk();
        $response->assertSee('AHMAD BIN ABDULLAH');
    }

    public function test_search_returns_multiple_matching_names(): void
    {
        $icService = app(IcNumberService::class);
        Participant::create([
            'full_name' => 'AHMAD BIN ALI',
            'ic_number' => $icService->encrypt('910101101234'),
            'ic_number_hash' => $icService->hash('910101101234'),
            'participant_number' => 'A002',
            'category' => '10KM',
            'imported_by' => User::first()->id,
            'is_active' => true,
        ]);

        $response = $this->post('/e-sijil/search', ['name' => 'Ahmad']);

        $response->assertOk();
        $response->assertSee('AHMAD BIN ABDULLAH');
        $response->assertSee('AHMAD BIN ALI');
        $response->assertSee('2 Rekod Peserta Ditemui');
    }

    public function test_short_name_shows_validation_error(): void
    {
        $this->post('/e-sijil/search', ['name' => 'A'])
            ->assertSessionHasErrors('name');
    }

    public function test_search_not_found_shows_message(): void
    {
        $response = $this->post('/e-sijil/search', [
            'name' => 'Nama Tiada Dalam Sistem',
        ]);

        $response->assertOk();
        $response->assertSee('tiada nama peserta yang sepadan');
    }

    public function test_ic_number_is_formatted(): void
    {
        $icService = app(IcNumberService::class);

        $this->assertEquals('900101-10-1234', $icService->format('900101101234'));
        $this->assertEquals('900101-10-1234', $icService->format('900101-10-1234'));
    }

    public function test_ic_normalization(): void
    {
        $icService = app(IcNumberService::class);

        $this->assertEquals('900101101234', $icService->normalize('900101-10-1234'));
        $this->assertEquals('900101101234', $icService->normalize('900101 10 1234'));
    }
}
