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

            /** keys */
            $table->id(); // primary key
            $table->unsignedInteger('calendar_id')->nullable()->unique();
            $table->unsignedInteger('subscription_id')->nullable()->unique();

            /** ui-specific fields */
            $table->string('title')->nullable();            // override name
            $table->string('color', 7)->nullable();         // background
            $table->string('color_fg', 7)->nullable();      // foreground
            $table->boolean('is_shared')->default(false);
            $table->boolean('is_remote')->default(false);   // local vs. subscription
            $table->json('settings')->nullable();           // arbitrary JSON
            $table->timestamps();

            /** foreign-key constraints */
            $table->foreign('calendar_id')
                  ->references('id')->on('calendars')
                  ->cascadeOnDelete();

            $table->foreign('subscription_id')
                  ->references('id')->on('calendarsubscriptions')
                  ->cascadeOnDelete();
        });
    }

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