<?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
    {
        // People: unique by (tax_number, social_security_number, deleted_at)
        Schema::table('person', function (Blueprint $table) {
            if (! self::hasIndex('person', 'person_identity_unique')) {
                $table->unique(['tax_number', 'social_security_number', 'deleted_at'], 'person_identity_unique');
            }
        });

        // Phones: unique by (person_id, nu, country_code, deleted_at)
        Schema::table('person_phones', function (Blueprint $table) {
            if (! self::hasIndex('person_phones', 'person_phones_unique')) {
                $table->unique(['person_id', 'nu', 'country_code', 'deleted_at'], 'person_phones_unique');
            }
        });

        // Addresses: unique by (person_id, address, country, deleted_at)
        Schema::table('person_addresses', function (Blueprint $table) {
            if (! self::hasIndex('person_addresses', 'person_addresses_unique')) {
                $table->unique(['person_id', 'address', 'country', 'deleted_at'], 'person_addresses_unique');
            }
        });

        // Contracts: unique by (client_case_id, reference, deleted_at)
        Schema::table('contracts', function (Blueprint $table) {
            if (! self::hasIndex('contracts', 'contracts_reference_unique')) {
                $table->unique(['client_case_id', 'reference', 'deleted_at'], 'contracts_reference_unique');
            }
        });

        // Accounts: unique by (contract_id, reference, deleted_at)
        Schema::table('accounts', function (Blueprint $table) {
            if (! self::hasIndex('accounts', 'accounts_reference_unique')) {
                $table->unique(['contract_id', 'reference', 'deleted_at'], 'accounts_reference_unique');
            }
        });
    }

    public function down(): void
    {
        Schema::table('person', function (Blueprint $table) {
            $table->dropUnique('person_identity_unique');
        });
        Schema::table('person_phones', function (Blueprint $table) {
            $table->dropUnique('person_phones_unique');
        });
        Schema::table('person_addresses', function (Blueprint $table) {
            $table->dropUnique('person_addresses_unique');
        });
        Schema::table('contracts', function (Blueprint $table) {
            $table->dropUnique('contracts_reference_unique');
        });
        Schema::table('accounts', function (Blueprint $table) {
            $table->dropUnique('accounts_reference_unique');
        });
    }

    private static function hasIndex(string $table, string $index): bool
    {
        // Attempt to detect index presence; if not supported, return false to try creating
        try {
            $connection = Schema::getConnection();
            $schemaManager = $connection->getDoctrineSchemaManager();
            $doctrineTable = $schemaManager->listTableDetails($table);

            return $doctrineTable->hasIndex($index);
        } catch (\Throwable $e) {
            return false;
        }
    }
};
