<?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('facturas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('caso_id')->constrained()->onDelete('cascade');
            $table->foreignId('ajustador_id')->constrained('ajustadores')->onDelete('cascade');
            $table->foreignId('aseguradora_id')->constrained('tenants')->onDelete('cascade');
            $table->string('numero_factura')->unique();
            $table->date('fecha');
            $table->decimal('subtotal', 12, 2);
            $table->decimal('iva', 12, 2)->default(0);
            $table->decimal('total', 12, 2);
            $table->integer('kilometraje')->nullable();
            $table->decimal('valor_kilometraje', 10, 4)->nullable();
            $table->enum('estado', ['borrador', 'emitida', 'pagada', 'rechazada'])->default('borrador');
            $table->date('fecha_pago')->nullable();
            $table->text('observaciones')->nullable();
            $table->timestamps();

            $table->index(['aseguradora_id', 'estado']);
        });
    }

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