<?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('macros', function (Blueprint $table) {
            $table->id();
            $table->string('uuid', 36)->unique();
            $table->string('name');
            $table->string('color', 7)->nullable();
            $table->boolean('trigger_on_startup')->default(false);
            $table->unsignedSmallInteger('image_type')->default(0);
            $table->unsignedInteger('action_count')->default(0);
            $table->timestamp('hidden_at')->nullable();
            $table->timestamp('last_imported_at')->nullable();
            $table->string('last_imported_filename')->nullable();
            $table->timestamps();
            $table->index('hidden_at');
        });

        Schema::create('macro_collections', function (Blueprint $table) {
            $table->id();
            $table->string('uuid', 36)->unique();
            $table->string('name');
            $table->timestamp('last_imported_at')->nullable();
            $table->timestamps();
        });

        Schema::create('macro_collection_macros', function (Blueprint $table) {
            $table->id();
            $table->foreignId('macro_collection_id')->constrained()->cascadeOnDelete();
            $table->foreignId('macro_id')->constrained('macros')->cascadeOnDelete();
            $table->unsignedInteger('order')->default(0);
            $table->timestamps();
            $table->unique(['macro_collection_id', 'macro_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('macro_collection_macros');
        Schema::dropIfExists('macro_collections');
        Schema::dropIfExists('macros');
    }
};
