<?php

namespace Tests\Feature;

use App\Models\Attendance;
use App\Models\Participant;
use App\Models\Program;
use App\Models\ProgramParticipant;
use App\Models\ProgramQrCode;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CheckinTest extends TestCase
{
    use RefreshDatabase;

    private Program $program;
    private ProgramQrCode $qrCode;

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

        $admin = User::factory()->create();
        $this->program = Program::factory()->published()->create(['created_by' => $admin->id]);

        $this->qrCode = ProgramQrCode::create([
            'program_id'    => $this->program->id,
            'token'         => 'test-qr-token-48-chars-xxxxxxxxxxxxxxxxxxxxxx',
            'qr_image_path' => 'qrcodes/test.png',
            'is_active'     => true,
        ]);
    }

    public function test_checkin_page_shows_for_published_program(): void
    {
        $this->get("/p/{$this->qrCode->token}")
            ->assertOk()
            ->assertSee('Check-In');
    }

    public function test_pre_registered_staff_can_check_in(): void
    {
        $participant = Participant::create([
            'name'             => 'Ahmad bin Abu',
            'no_kp'            => '900101011234',
            'email'            => 'ahmad@test.com',
            'participant_type' => 'staff',
        ]);

        ProgramParticipant::create([
            'program_id'             => $this->program->id,
            'participant_id'         => $participant->id,
            'registration_source'    => 'pre_registered',
            'is_pre_registered'      => true,
            'pre_registered_session' => 'pagi',
            'status'                 => 'registered',
        ]);

        $this->post("/p/{$this->qrCode->token}/staff", [
            'no_kp' => '900101011234',
        ])->assertViewIs('public.checkin.success');

        $this->assertDatabaseHas('attendances', [
            'program_id'     => $this->program->id,
            'participant_id' => $participant->id,
        ]);
    }

    public function test_duplicate_checkin_shows_already_view(): void
    {
        $participant = Participant::create([
            'name'             => 'Siti binti Ali',
            'no_kp'            => '950202022345',
            'participant_type' => 'staff',
        ]);

        ProgramParticipant::create([
            'program_id'             => $this->program->id,
            'participant_id'         => $participant->id,
            'registration_source'    => 'pre_registered',
            'is_pre_registered'      => true,
            'pre_registered_session' => 'petang',
            'status'                 => 'checked_in',
        ]);

        Attendance::create([
            'program_id'         => $this->program->id,
            'participant_id'     => $participant->id,
            'attendance_session' => 'petang',
            'attendance_source'  => 'pre_registered_staff',
            'checked_in_at'      => now(),
        ]);

        $this->post("/p/{$this->qrCode->token}/staff", [
            'no_kp' => '950202022345',
        ])->assertViewIs('public.checkin.already');
    }

    public function test_walk_in_registration_succeeds(): void
    {
        $this->post("/p/{$this->qrCode->token}/external", [
            'name'   => 'Orang Luar',
            'no_kp'  => '880303033456',
            'email'  => 'luaran@test.com',
            'phone'  => '0123456789',
            'agency' => 'Syarikat Luar',
        ])->assertViewIs('public.checkin.success');

        $this->assertDatabaseHas('participants', ['no_kp' => '880303033456']);
        $this->assertDatabaseHas('attendances', ['attendance_source' => 'walk_in_external']);
    }
}
