<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Insurance Companies
        Schema::create('insurance_companies', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('contact_person')->nullable();
            $table->text('notes')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 2. Vehicle Insurances
        Schema::create('vehicle_insurances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('car_id')->constrained('cars')->onDelete('cascade');
            $table->foreignId('insurance_company_id')->constrained('insurance_companies');
            $table->string('policy_number')->unique();
            $table->enum('insurance_type', ['comprehensive', 'third_party']);
            $table->decimal('coverage_amount', 12, 2)->nullable();
            $table->decimal('deductible_amount', 10, 2)->default(0);
            $table->date('start_date');
            $table->date('end_date');
            $table->decimal('premium_amount', 10, 2);
            $table->enum('payment_status', ['paid', 'unpaid', 'partial'])->default('unpaid');
            $table->string('document_path')->nullable();
            $table->text('notes')->nullable();
            $table->enum('status', ['active', 'expired', 'cancelled'])->default('active');
            $table->foreignId('created_by')->nullable()->constrained('users');
            $table->timestamps();
            
            $table->index(['car_id', 'status', 'end_date']);
        });

        // 3. Insurance Claims
        Schema::create('insurance_claims', function (Blueprint $table) {
            $table->id();
            $table->foreignId('car_id')->constrained('cars')->onDelete('cascade');
            $table->foreignId('rental_id')->nullable()->constrained('rentals'); // Link to contract if available
            $table->string('claim_number')->unique();
            $table->date('accident_date');
            $table->text('description');
            $table->decimal('estimated_damage_cost', 12, 2)->nullable();
            $table->decimal('insurance_coverage_amount', 12, 2)->nullable();
            $table->decimal('deductible_paid', 10, 2)->default(0);
            $table->enum('claim_status', ['pending', 'accepted', 'rejected', 'closed'])->default('pending');
            $table->decimal('settlement_amount', 12, 2)->nullable();
            $table->text('attachments')->nullable(); // JSON list of files
            $table->foreignId('created_by')->nullable()->constrained('users');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('insurance_claims');
        Schema::dropIfExists('vehicle_insurances');
        Schema::dropIfExists('insurance_companies');
    }
};
