<?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', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->onDelete('cascade');
            $table->string('numero_reclamo')->nullable();
            $table->string('numero_caso_sias')->nullable();
            $table->string('numero_caso_wan_ax')->nullable();
            $table->date('fecha_siniestro');
            $table->date('fecha_asignacion')->nullable();
            $table->date('fecha_cierre')->nullable();
            $table->enum('cobertura', ['DP', 'RC'])->comment('DP=Daños Propios, RC=Responsabilidad Civil');
            $table->enum('estado_actual', [
                'pendiente_coordinacion',
                'coordinado',
                'caso_en_ruta',
                'recoordinacion',
                'no_contesta',
                'llamar_despues',
                'pendiente_proforma_cliente',
                'pendiente_proforma_taller',
                'pendiente_aviso_siniestro',
                'vehiculo_retenido',
                'vehiculo_circulando',
                'vehiculo_no_taller',
                'analisis',
                'inspeccion_realizada',
                'inspeccion_no_realizada',
                'elaboracion_informe',
                'confirmacion_cobertura',
                'autorizacion_inspector',
                'autorizacion_reinspector',
                'autorizacion_ejecutivo',
                'autorizacion_director',
                'autorizacion_gerente',
                'mesa_compras',
                'en_reparacion',
                'alcance',
                'facturacion',
                'finalizado',
                'rechazado',
            ])->default('pendiente_coordinacion');
            $table->text('observaciones')->nullable();
            $table->string('categoria_documento')->nullable();
            $table->string('tipo_ajuste')->nullable();
            $table->integer('tiempo_total_minutos')->nullable();
            $table->string('link_fotos')->nullable();
            $table->string('link_informe')->nullable();
            $table->enum('nivel_dano', ['alta', 'media', 'baja'])->nullable();
            $table->boolean('aplica_kilometraje')->default(false);
            $table->integer('kilometros')->nullable();
            $table->decimal('tarifa_kilometro', 10, 4)->nullable();
            $table->string('ciudad_destino')->nullable();

            // Relaciones
            $table->foreignId('cliente_id')->constrained('clientes')->onDelete('cascade');
            $table->foreignId('vehiculo_id')->constrained('vehiculos')->onDelete('cascade');
            $table->foreignId('aseguradora_id')->constrained('tenants')->onDelete('cascade');
            $table->foreignId('broker_id')->nullable()->constrained('brokers')->onDelete('set null');
            $table->foreignId('taller_id')->nullable()->constrained('talleres')->onDelete('set null');
            $table->foreignId('ajustador_id')->nullable()->constrained('ajustadores')->onDelete('set null');
            $table->foreignId('ejecutivo_id')->nullable()->constrained('personal_zurich')->onDelete('set null');
            $table->foreignId('director_id')->nullable()->constrained('personal_zurich')->onDelete('set null');
            $table->foreignId('reinspector_id')->nullable()->constrained('personal_zurich')->onDelete('set null');

            $table->timestamps();

            $table->index(['tenant_id', 'estado_actual']);
            $table->index(['tenant_id', 'fecha_siniestro']);
        });
    }

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