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

            // FK = PK → calendarobjects.id
            $table->unsignedInteger('event_id')->primary();

            // cached fields from the VEVENT
            $table->string('title')->nullable();
            $table->text('description')->nullable();

            // free-text fallback if no geocoded location
            $table->string('location')->nullable();

            // foreign-key to the richer locations table
            $table->unsignedBigInteger('location_id')->nullable();
            $table->foreign('location_id')
                  ->references('id')
                  ->on('locations')
                  ->nullOnDelete();

            // all-day flag
            $table->boolean('all_day')->default(false);

            // extra metadata
            $table->string('category')->nullable();
            $table->string('reminder_minutes')->nullable();
            $table->boolean('is_private')->default(false);
            $table->dateTime('start_at')->nullable();
            $table->dateTime('end_at')->nullable();
            $table->json('extra')->nullable();
            $table->timestamps();

            // FK to Sabre calendarobjects table
            $table->foreign('event_id')
                  ->references('id')
                  ->on('calendarobjects')
                  ->cascadeOnDelete();
        });
    }

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