<?php

namespace Database\Seeders;

use App\Models\MeetupBooth;
use App\Models\MeetupEvent;
use App\Models\MeetupParticipant;
use App\Models\MeetupRole;
use App\Models\MeetupSession;
use Illuminate\Database\Seeder;

class MeetupSeeder extends Seeder
{
    public function run(): void
    {
        $event = MeetupEvent::query()->updateOrCreate(
            ['id' => 'icaiia2026'],
            [
                'name' => 'ICAIIA 2026',
                'venue' => 'Putrajaya, MY',
                'dates' => '8 – 10 October 2026',
                'event_name' => 'ICAIIA 2026',
                'signer' => 'Organising Chairperson',
                'passcode' => '1234',
                'organiser_username' => 'organiser@icaiia2026.com',
                'organiser_password' => '1234',
                'is_virtual' => false,
                'starts_at' => '2026-10-08 00:00:00',
                'ends_at' => '2026-10-10 23:59:59',
            ]
        );

        $roles = [
            'VISITOR' => ['sessions' => 5, 'stamps' => 5, 'note' => 'Up to 5 sessions'],
            'AUTHOR' => ['sessions' => 10, 'stamps' => 8, 'note' => 'Up to 10 sessions'],
            'PRESENTER' => ['sessions' => null, 'stamps' => 8, 'note' => 'Unlimited sessions'],
            'KEYNOTE SPEAKER' => ['sessions' => null, 'stamps' => 0, 'note' => 'Unlimited sessions'],
            'STUDENT' => ['sessions' => 5, 'stamps' => 5, 'note' => 'Up to 5 sessions'],
            'EXHIBITOR' => ['sessions' => null, 'stamps' => 0, 'note' => 'Selected sessions only'],
            'VIP' => ['sessions' => null, 'stamps' => 0, 'note' => 'Unlimited sessions'],
            'COMMITTEE' => ['sessions' => null, 'stamps' => 0, 'note' => 'Attendance check-in access'],
            'ORGANISER' => ['sessions' => null, 'stamps' => 0, 'note' => 'Organiser / committee access'],
        ];

        foreach ($roles as $name => $cfg) {
            MeetupRole::query()->updateOrCreate(
                ['event_id' => $event->id, 'name' => $name],
                [
                    'sessions_limit' => $cfg['sessions'],
                    'stamps_required' => $cfg['stamps'],
                    'note' => $cfg['note'],
                ]
            );
        }

        $booths = [
            ['B01', 'Booth 01', '01'],
            ['B02', 'Booth 02', '02'],
            ['B03', 'Booth 03', '03'],
            ['B04', 'Booth 04', '04'],
            ['B05', 'Booth 05', '05'],
            ['B06', 'Booth 06', '06'],
            ['B07', 'Booth 07', '07'],
            ['B08', 'Booth 08', '08'],
        ];

        MeetupBooth::query()->where('event_id', $event->id)->delete();
        foreach ($booths as [$code, $name, $mark]) {
            MeetupBooth::query()->create([
                'event_id' => $event->id,
                'code' => $code,
                'name' => $name,
                'mark' => $mark,
            ]);
        }

        $sessions = [
            ['S1', '2026-10-08', '09:00-10:00', 'Keynote: AI for a Better Future', 'Prof. Dr. Rui Martins', 'Main Hall', []],
            ['S2', '2026-10-08', '10:30-11:30', 'AI for Healthcare: Opportunities and Challenges', 'Dr. Aina Rahim', 'Main Hall', []],
            ['S3', '2026-10-08', '13:30-14:30', 'Data Science in Action', 'Ir. Tan Wei', 'Hall B', ['PRESENTER', 'AUTHOR', 'KEYNOTE SPEAKER', 'VIP']],
            ['S4', '2026-10-09', '09:00-10:00', 'Human-Centred Design for Serious Games', 'Dr. Lim Mei', 'Hall A', []],
            ['S5', '2026-10-09', '11:00-12:00', 'Exhibitor Networking Lunch', 'Organising Committee', 'Foyer', ['EXHIBITOR', 'VIP']],
            ['S6', '2026-10-10', '09:30-10:30', 'Closing Panel: The Next Decade', 'Panel', 'Main Hall', []],
        ];

        MeetupSession::query()->where('event_id', $event->id)->delete();
        foreach ($sessions as [$key, $day, $time, $title, $speaker, $venue, $allowed]) {
            MeetupSession::query()->create([
                'event_id' => $event->id,
                'session_key' => $key,
                'day' => $day,
                'time' => $time,
                'title' => $title,
                'speaker' => $speaker,
                'venue' => $venue,
                'allowed_roles' => $allowed,
            ]);
        }

        $demoParticipants = [
            ['siti@ump.edu.my', 'Siti Normaziah Ihsan', 'UMPSA', '900101123456', '0123456789', 'PRESENTER'],
            ['ali@example.com', 'Ali bin Ahmad', 'Tech Solutions Sdn Bhd', '880212065432', '0198887766', 'VISITOR'],
            ['maya@company.com', 'Maya Lee', 'Company Sdn Bhd', '950505101234', '0112223344', 'AUTHOR'],
            ['committee@icaiia2026.com', 'Event Committee', 'ICAIIA', '910303145678', '0135556677', 'VISITOR', true],
        ];

        foreach ($demoParticipants as $row) {
            [$email, $name, $org, $ic, $phone, $role] = $row;
            $isCommittee = (bool) ($row[6] ?? false);
            $slug = strtolower(preg_replace('/[^a-z0-9]/', '_', trim(strtolower($email))));
            MeetupParticipant::query()->updateOrCreate(
                ['event_id' => $event->id, 'slug' => $slug],
                [
                    'email' => $email,
                    'name' => $name,
                    'org' => $org,
                    'ic' => $ic,
                    'phone' => $phone,
                    'role' => $role,
                    'is_committee' => $isCommittee,
                    'booth_stamps' => [],
                    'session_attendance' => [],
                ]
            );
        }
    }
}
