<?php

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

/**
 * Altera colunas PII para TEXT (necessario para valores encriptados que sao
 * significativamente maiores que o plaintext original) e adiciona colunas
 * nif_hash para pesquisa por NIF em campos encriptados.
 */
return new class extends Migration
{
    public function up(): void
    {
        // ── submissions ──────────────────────────────────────────
        // Dropar indices que referenciam colunas que vao passar a TEXT
        Schema::table('submissions', function (Blueprint $table) {
            $table->dropIndex('submissions_nif_is_latest_index');
            $table->dropIndex('submissions_nif_index');
        });

        Schema::table('submissions', function (Blueprint $table) {
            $table->text('nome_completo')->change();
            $table->text('nif')->change();
            $table->text('email')->change();
            $table->text('telemovel')->change();
            $table->text('contacto_emergencia_nome')->change();
            $table->text('contacto_emergencia_relacao')->change();
            $table->text('contacto_emergencia_telefone')->change();
            $table->text('ip_address')->nullable()->change();

            $table->string('nif_hash', 64)->nullable()->after('nif');
            $table->index('nif_hash', 'idx_submissions_nif_hash');
            $table->index(['nif_hash', 'is_latest'], 'idx_submissions_nif_hash_latest');
        });

        // ── employees_snapshot ───────────────────────────────────
        // Dropar unique constraint no nif antes de mudar para TEXT
        Schema::table('employees_snapshot', function (Blueprint $table) {
            $table->dropUnique('employees_snapshot_nif_unique');
        });

        Schema::table('employees_snapshot', function (Blueprint $table) {
            $table->text('nome_completo')->change();
            $table->text('nif')->change();
            $table->text('email')->nullable()->change();
            $table->text('telemovel')->nullable()->change();
            $table->text('morada')->nullable()->change();
            $table->text('contacto_emergencia_nome')->nullable()->change();
            $table->text('contacto_emergencia_relacao')->nullable()->change();
            $table->text('contacto_emergencia_telefone')->nullable()->change();

            $table->string('nif_hash', 64)->nullable()->after('nif');
            $table->unique('nif_hash', 'idx_snapshot_nif_hash_unique');
        });

        // ── rgpd_requests ────────────────────────────────────────
        Schema::table('rgpd_requests', function (Blueprint $table) {
            $table->text('nome_completo')->change();
            $table->text('email')->change();
            $table->text('nif')->nullable()->change();
        });

        // ── audit_logs ───────────────────────────────────────────
        Schema::table('audit_logs', function (Blueprint $table) {
            $table->longText('old_values')->nullable()->change();
            $table->longText('new_values')->nullable()->change();
            $table->text('ip_address')->nullable()->change();
        });
    }

    public function down(): void
    {
        Schema::table('submissions', function (Blueprint $table) {
            $table->dropIndex('idx_submissions_nif_hash_latest');
            $table->dropIndex('idx_submissions_nif_hash');
            $table->dropColumn('nif_hash');

            $table->string('nome_completo')->change();
            $table->string('nif', 20)->change();
            $table->string('email')->change();
            $table->string('telemovel', 20)->change();
            $table->string('contacto_emergencia_nome')->change();
            $table->string('contacto_emergencia_relacao')->change();
            $table->string('contacto_emergencia_telefone', 20)->change();
            $table->string('ip_address', 45)->nullable()->change();

            $table->index('nif', 'submissions_nif_index');
            $table->index(['nif', 'is_latest'], 'submissions_nif_is_latest_index');
        });

        Schema::table('employees_snapshot', function (Blueprint $table) {
            $table->dropUnique('idx_snapshot_nif_hash_unique');
            $table->dropColumn('nif_hash');

            $table->string('nome_completo')->change();
            $table->string('nif', 20)->change();
            $table->string('email')->nullable()->change();
            $table->string('telemovel', 20)->nullable()->change();
            $table->text('morada')->nullable()->change();
            $table->string('contacto_emergencia_nome')->nullable()->change();
            $table->string('contacto_emergencia_relacao')->nullable()->change();
            $table->string('contacto_emergencia_telefone', 20)->nullable()->change();

            $table->unique('nif', 'employees_snapshot_nif_unique');
        });

        Schema::table('rgpd_requests', function (Blueprint $table) {
            $table->string('nome_completo')->change();
            $table->string('email')->change();
            $table->string('nif', 20)->nullable()->change();
        });

        Schema::table('audit_logs', function (Blueprint $table) {
            $table->json('old_values')->nullable()->change();
            $table->json('new_values')->nullable()->change();
            $table->string('ip_address', 45)->nullable()->change();
        });
    }
};
