<?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
    {
        // --- email_senders ---
        // Drop FK and column first (while table still has original name so constraint name matches)
        Schema::table('solicitor_email_senders', function (Blueprint $table) {
            $table->dropForeign(['solicitor_id']);
            $table->dropColumn('solicitor_id');
        });

        Schema::rename('solicitor_email_senders', 'email_senders');

        // --- phone_numbers ---
        // Drop FK and composite indexes before renaming
        Schema::table('solicitor_phone_numbers', function (Blueprint $table) {
            $table->dropForeign(['solicitor_id']);
            $table->dropIndex(['solicitor_id', 'phone_number_type_id']);
            $table->dropIndex(['solicitor_id', 'is_active']);
            $table->dropColumn('solicitor_id');
        });

        Schema::rename('solicitor_phone_numbers', 'phone_numbers');
    }

    public function down(): void
    {
        Schema::rename('phone_numbers', 'solicitor_phone_numbers');

        Schema::table('solicitor_phone_numbers', function (Blueprint $table) {
            $table->foreignId('solicitor_id')->nullable()->constrained()->cascadeOnDelete();
        });

        Schema::rename('email_senders', 'solicitor_email_senders');

        Schema::table('solicitor_email_senders', function (Blueprint $table) {
            $table->foreignId('solicitor_id')->nullable()->constrained()->cascadeOnDelete();
        });
    }
};
