<?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('casos_prejuridicos', function (Blueprint $table) {
            $table->id();
            $table->string('radicado', 30)->unique();
            $table->foreignId('deudor_id')->nullable()->constrained('deudores')->nullOnDelete();
            $table->foreignId('cliente_corporativo_id')->nullable()->constrained('clientes_corporativos')->nullOnDelete();
            $table->foreignId('responsable_id')->nullable()->constrained('users')->nullOnDelete();
            $table->enum('etapa', [
                'recepcion','registro','localizacion','gestion_activa','acuerdo_en_curso','seguimiento_acuerdo'
            ])->default('recepcion');
            $table->enum('estado', ['activo','cerrado_pago','cerrado_acuerdo','judicializado','archivado'])->default('activo');
            $table->decimal('saldo_capital', 15, 2)->default(0);
            $table->decimal('intereses', 15, 2)->default(0);
            $table->decimal('total_deuda', 15, 2)->default(0);
            $table->integer('dias_mora')->default(0);
            $table->date('fecha_recepcion')->nullable();
            $table->foreignId('proceso_juridico_id')->nullable()->constrained('procesos')->nullOnDelete();
            $table->date('fecha_judicializacion')->nullable();
            $table->text('observaciones')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['etapa','estado']);
            $table->index('deudor_id');
        });
    }

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