<?php

namespace Tests\Feature\Mexco;

use App\Models\MeetupEvent;
use App\Models\MeetupParticipant;
use App\Models\MeetupRole;
use App\Services\MeetupService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AttendCommitteeLoginTest extends TestCase
{
    use RefreshDatabase;

    protected MeetupService $meetup;

    protected function setUp(): void
    {
        parent::setUp();
        $this->meetup = app(MeetupService::class);
    }

    protected function seedEventWithPeople(): MeetupEvent
    {
        $event = MeetupEvent::query()->create([
            'id' => 'attend-demo',
            'name' => 'Attend Demo Event',
            'event_name' => 'Attend Demo Event',
            'short_name' => 'Attend Demo',
            'organiser_password' => 'secret-org',
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(7),
        ]);

        foreach (['VISITOR', 'COMMITTEE', 'ORGANISER'] as $role) {
            MeetupRole::query()->create([
                'event_id' => $event->id,
                'name' => $role,
                'sessions_limit' => null,
                'stamps_required' => 0,
            ]);
        }

        MeetupParticipant::query()->create([
            'event_id' => $event->id,
            'slug' => 'visitor_demo_mexco',
            'email' => 'visitor@demo.mexco',
            'name' => 'Demo Visitor',
            'ic' => '900101123456',
            'role' => 'VISITOR',
            'booth_stamps' => [],
            'session_attendance' => [],
        ]);

        MeetupParticipant::query()->create([
            'event_id' => $event->id,
            'slug' => 'committee_demo_mexco',
            'email' => 'committee@demo.mexco',
            'name' => 'Demo Committee',
            'ic' => '910303145678',
            'role' => 'VISITOR',
            'is_committee' => true,
            'booth_stamps' => [],
            'session_attendance' => [],
        ]);

        return $event;
    }

    public function test_committee_login_returns_only_related_events(): void
    {
        $this->seedEventWithPeople();

        $response = $this->postJson('/mexco/api/auth/attend', [
            'email' => 'committee@demo.mexco',
            'ic' => '910303145678',
        ]);

        $response->assertOk()
            ->assertJsonPath('ok', true)
            ->assertJsonPath('email', 'committee@demo.mexco')
            ->assertJsonCount(1, 'matches')
            ->assertJsonPath('matches.0.eventId', 'attend-demo')
            ->assertJsonPath('matches.0.role', 'VISITOR')
            ->assertJsonPath('matches.0.is_committee', true);
    }

    public function test_visitor_cannot_use_attend_login(): void
    {
        $this->seedEventWithPeople();

        $this->postJson('/mexco/api/auth/attend', [
            'email' => 'visitor@demo.mexco',
            'ic' => '900101123456',
        ])->assertStatus(422)
            ->assertJsonPath('ok', false);
    }

    public function test_committee_can_enter_related_event_and_verify(): void
    {
        $event = $this->seedEventWithPeople();

        $this->postJson('/mexco/api/auth/attend', [
            'email' => 'committee@demo.mexco',
            'ic' => '910303145678',
        ])->assertOk();

        $this->postJson('/mexco/api/events/'.$event->id.'/attend')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->postJson('/mexco/api/events/'.$event->id.'/verify/visitor_demo_mexco')
            ->assertOk()
            ->assertJsonPath('ok', true);
    }

    public function test_committee_cannot_enter_unrelated_event(): void
    {
        $this->seedEventWithPeople();

        MeetupEvent::query()->create([
            'id' => 'other-event',
            'name' => 'Other Event',
            'organiser_password' => 'x',
        ]);

        $this->postJson('/mexco/api/auth/attend', [
            'email' => 'committee@demo.mexco',
            'ic' => '910303145678',
        ])->assertOk();

        $this->postJson('/mexco/api/events/other-event/attend')
            ->assertStatus(403);
    }
}
