<?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('event_attendees', function (Blueprint $table) {
            $table->id();

            $table->unsignedInteger('event_id');
            $table->foreign('event_id')
                ->references('id')
                ->on('calendarobjects')
                ->cascadeOnDelete();

            $table->ulid('attendee_user_id')->nullable();
            $table->foreign('attendee_user_id')
                ->references('id')
                ->on('users')
                ->nullOnDelete();

            $table->string('attendee_uri', 500)->nullable();
            $table->string('email')->nullable();
            $table->string('name')->nullable();
            $table->string('role', 32)->nullable();
            $table->string('partstat', 32)->nullable();
            $table->string('cutype', 32)->nullable();
            $table->boolean('rsvp')->nullable();
            $table->boolean('is_organizer')->default(false);
            $table->json('extra')->nullable();
            $table->timestamps();

            $table->index(['event_id', 'email']);
            $table->index('attendee_user_id');
            $table->unique(['event_id', 'attendee_uri']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('event_attendees');
    }
};
