<?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::dropIfExists('custom_reports');

        Schema::create('custom_reports', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('name', 200);
            $table->text('description')->nullable();
            $table->json('ir')->comment('Report IR (JSON spec)');
            $table->text('last_compiled_sql')->nullable()->comment('Debug — last compiled SQL string');
            $table->timestamp('last_executed_at')->nullable();
            $table->boolean('is_public')->default(false);
            $table->foreignId('owner_admin_id')->constrained('admins')->cascadeOnDelete();
            $table->timestamps();

            $table->index(['owner_admin_id', 'is_public']);
        });
    }

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