<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('meetup_events', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->string('name');
            $table->string('venue')->nullable();
            $table->string('dates')->nullable();
            $table->string('event_name')->nullable();
            $table->string('signer')->default('Organising Chairperson');
            $table->string('signer_title')->nullable();
            $table->string('mail_endpoint')->nullable();
            $table->string('passcode')->default('1234');
            $table->timestamps();
        });

        Schema::create('meetup_roles', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->string('name');
            $table->unsignedInteger('sessions_limit')->nullable();
            $table->unsignedInteger('stamps_required')->default(0);
            $table->string('note')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique(['event_id', 'name']);
        });

        Schema::create('meetup_booths', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->string('code');
            $table->string('name');
            $table->string('mark', 3);
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique(['event_id', 'code']);
        });

        Schema::create('meetup_sessions', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->string('session_key');
            $table->date('day');
            $table->string('time');
            $table->string('title');
            $table->string('speaker')->nullable();
            $table->string('venue')->nullable();
            $table->json('allowed_roles')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique(['event_id', 'session_key']);
        });

        Schema::create('meetup_participants', function (Blueprint $table) {
            $table->id();
            $table->string('event_id');
            $table->string('slug');
            $table->string('email');
            $table->string('name');
            $table->string('org')->nullable();
            $table->string('ic')->nullable();
            $table->string('phone')->nullable();
            $table->string('role')->default('VISITOR');
            $table->timestamp('verified_at')->nullable();
            $table->json('booth_stamps')->nullable();
            $table->json('session_attendance')->nullable();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
            $table->unique(['event_id', 'slug']);
            $table->index(['event_id', 'email']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('meetup_participants');
        Schema::dropIfExists('meetup_sessions');
        Schema::dropIfExists('meetup_booths');
        Schema::dropIfExists('meetup_roles');
        Schema::dropIfExists('meetup_events');
    }
};
