<?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('tenant_main_carousel_images');

        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropColumn('hero_bg_image_id');
            $table->dropColumn(['hero_config', 'event_config']);
        });
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->json('hero_config')->nullable();
            $table->json('event_config')->nullable();
            $table->unsignedBigInteger('hero_bg_image_id')
                ->virtualAs('hero_config->>"$.background_image_id"')
                ->nullable();
        });

        Schema::create('tenant_main_carousel_images', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')
                ->constrained('tenants')
                ->cascadeOnDelete();
            $table->foreignId('attachment_id')
                ->constrained('attachments')
                ->cascadeOnDelete();
            $table->unsignedInteger('orden')->default(0);
            $table->timestamps();

            $table->unique(['tenant_id', 'attachment_id']);
            $table->index(['tenant_id', 'orden']);
        });
    }
};
