<?php

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

// Depends on: split_vat_folder_type (2026_03_09_000003) which references DB::table('folders').
// This migration MUST run after that one — timestamp ordering guarantees this.
return new class extends Migration
{
    public function up(): void
    {
        // 1. Drop FK constraints (use column array for SQLite compatibility)
        Schema::table('messages', function (Blueprint $table) {
            $table->dropForeign(['folder_id']);
        });
        Schema::table('folder_invitations', function (Blueprint $table) {
            $table->dropForeign(['folder_id']);
        });

        // 2. Drop old index (must happen before column rename for cross-driver safety)
        Schema::table('messages', function (Blueprint $table) {
            $table->dropIndex(['folder_id', 'created_at']);
        });

        // 3. Rename columns
        Schema::table('messages', function (Blueprint $table) {
            $table->renameColumn('folder_id', 'declaration_id');
        });
        Schema::table('folder_invitations', function (Blueprint $table) {
            $table->renameColumn('folder_id', 'declaration_id');
        });

        // 4. Create new index with renamed column
        Schema::table('messages', function (Blueprint $table) {
            $table->index(['declaration_id', 'created_at']);
        });

        // 5-6. Rename tables
        Schema::rename('folders', 'declarations');
        Schema::rename('folder_invitations', 'declaration_invitations');

        // 7. Re-add FK constraints
        Schema::table('messages', function (Blueprint $table) {
            $table->foreign('declaration_id')->references('id')->on('declarations')->cascadeOnDelete();
        });
        Schema::table('declaration_invitations', function (Blueprint $table) {
            $table->foreign('declaration_id')->references('id')->on('declarations')->cascadeOnDelete();
        });
    }

    public function down(): void
    {
        // 1. Drop FK constraints (use column array for SQLite compatibility)
        Schema::table('messages', function (Blueprint $table) {
            $table->dropForeign(['declaration_id']);
        });
        Schema::table('declaration_invitations', function (Blueprint $table) {
            $table->dropForeign(['declaration_id']);
        });

        // 2-3. Rename tables back
        Schema::rename('declaration_invitations', 'folder_invitations');
        Schema::rename('declarations', 'folders');

        // 4. Drop new index
        Schema::table('messages', function (Blueprint $table) {
            $table->dropIndex(['declaration_id', 'created_at']);
        });

        // 5. Rename columns back
        Schema::table('messages', function (Blueprint $table) {
            $table->renameColumn('declaration_id', 'folder_id');
        });
        Schema::table('folder_invitations', function (Blueprint $table) {
            $table->renameColumn('declaration_id', 'folder_id');
        });

        // 6. Recreate old index
        Schema::table('messages', function (Blueprint $table) {
            $table->index(['folder_id', 'created_at']);
        });

        // 7. Re-add FK constraints
        Schema::table('messages', function (Blueprint $table) {
            $table->foreign('folder_id')->references('id')->on('folders')->cascadeOnDelete();
        });
        Schema::table('folder_invitations', function (Blueprint $table) {
            $table->foreign('folder_id')->references('id')->on('folders')->cascadeOnDelete();
        });
    }
};
