<?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::create('submissions', function (Blueprint $table) {
            $table->id();
            $table->enum('type', ['update', 'new_admission']);

            // Identificação
            $table->string('nome_completo');
            $table->string('nif', 20)->index();
            $table->date('data_nascimento');

            // Contato
            $table->string('email');
            $table->string('telemovel', 20);
            $table->string('contacto_emergencia_nome');
            $table->string('contacto_emergencia_relacao');
            $table->string('contacto_emergencia_telefone', 20);

            // Morada
            $table->text('morada_original');
            $table->text('morada_corrigida')->nullable();
            $table->boolean('mudou_morada')->default(false);

            // Fiscal/Familiar
            $table->string('estado_civil', 50);
            $table->unsignedTinyInteger('nr_dependentes')->default(0);

            // Habilitações
            $table->string('habilitacoes_literarias', 100);

            // Comprovativo morada
            $table->string('comprovativo_path')->nullable();
            $table->string('comprovativo_original_name')->nullable();
            $table->string('comprovativo_mime')->nullable();
            $table->unsignedInteger('comprovativo_size')->nullable();

            // Estado
            $table->enum('status', [
                'novo',
                'pendente',
                'sem_alteracoes_auto',
                'aprovado',
                'rejeitado',
                'concluido',
                'bloqueado',
            ])->default('novo');

            // Flags
            $table->boolean('flag_sem_correspondencia')->default(false);
            $table->boolean('flag_ip_fora_pt')->default(false);
            $table->boolean('flag_inconsistencia')->default(false);
            $table->boolean('flag_suspeito')->default(false);
            $table->unsignedTinyInteger('suspicion_score')->default(0);

            // Snapshot match
            $table->foreignId('snapshot_match_id')->nullable()->constrained('employees_snapshot')->nullOnDelete();
            $table->json('diff_data')->nullable(); // JSON com diferenças campo a campo

            // Metadata
            $table->boolean('is_latest')->default(true);
            $table->string('ip_address', 45)->nullable();
            $table->string('ip_hash', 64)->nullable();
            $table->string('user_agent')->nullable();
            $table->text('rejection_reason')->nullable();

            // Declaração
            $table->boolean('declaracao_verdade')->default(false);

            $table->timestamps();

            $table->index(['nif', 'is_latest']);
            $table->index(['status']);
            $table->index(['created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('submissions');
    }
};
