<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('serenata_route_plans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained()->cascadeOnDelete();
            // `serenatas` is created in a later migration, so this FK is added afterwards.
            $table->unsignedBigInteger('serenata_id');
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->date('plan_date');
            $table->string('city', 120)->nullable();
            $table->string('mariachi_key', 120);
            $table->string('mariachi_label', 190)->nullable();
            $table->string('column_key', 120)->nullable();
            $table->string('column_label', 120)->nullable();
            $table->unsignedInteger('route_order')->default(0);
            $table->timestamps();

            $table->unique(['organization_id', 'serenata_id', 'plan_date', 'mariachi_key'], 'serenata_route_unique');
            $table->index(['organization_id', 'plan_date', 'city', 'mariachi_key'], 'serenata_route_filter_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('serenata_route_plans');
    }
};
