<?php

use App\Domains\Catalog\Enums\ProductLayout;
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('featured_items');
        Schema::dropIfExists('featured_products');
        Schema::dropIfExists('group_items');
        Schema::dropIfExists('featured_variants');
        Schema::dropIfExists('featured_groups');

        Schema::create('featured_groups', function (Blueprint $table): void {
            $table->id();
            $table->string('tenant_code');
            $table->enum('product_layout', ProductLayout::values());
            $table->string('group_name');
            $table->unsignedInteger('group_order')->default(0);

            $table->foreign('tenant_code')
                ->references('codigo')
                ->on('tenants')
                ->cascadeOnUpdate()
                ->restrictOnDelete();
        });

        Schema::create('featured_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('featured_group_id')
                ->constrained('featured_groups')
                ->cascadeOnDelete();
            $table->foreignId('catalog_item_id')
                ->constrained('catalog_items')
                ->cascadeOnDelete();
            $table->unsignedInteger('order')->default(0);
        });
    }

    public function down(): void
    {
        throw new RuntimeException('This destructive featured catalog migration cannot be reversed.');
    }
};
