<?php

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

/**
 * Insurance (companies + claims) and warranties (spec §5.7 / §6.11). An
 * approved claim splits billing into the insurer's approved amount and the
 * customer's excess (which becomes the customer payable on the ERP invoice).
 * Warranties are created from repair-order lines carrying warranty_months.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_insurance_companies')) {
            Schema::create('vrc_insurance_companies', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('contact', 191)->nullable();
                $t->string('phone', 30)->nullable();
                $t->string('email', 120)->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
            });
        }

        if (! Schema::hasTable('vrc_insurance_claims')) {
            Schema::create('vrc_insurance_claims', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('repair_order_id');
                $t->unsignedBigInteger('insurance_company_id');
                $t->string('policy_no', 80)->nullable();
                $t->string('claim_no', 80)->nullable();
                $t->string('status', 20)->default('draft'); // draft|submitted|approved|partially_approved|rejected|settled
                $t->decimal('approved_amount', 15, 2)->nullable();
                $t->decimal('excess_amount', 15, 2)->nullable(); // customer pays
                $t->dateTime('submitted_at')->nullable();
                $t->dateTime('settled_at')->nullable();
                $t->text('notes')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'repair_order_id'], 'vrc_ins_claim_biz_ro_idx');
            });
        }

        if (! Schema::hasTable('vrc_warranties')) {
            Schema::create('vrc_warranties', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('repair_order_id');
                $t->unsignedBigInteger('vehicle_id')->nullable();
                $t->string('kind', 10)->default('part'); // part|service
                $t->unsignedBigInteger('ref_id')->nullable(); // line id
                $t->string('description', 255);
                $t->date('start_date');
                $t->date('end_date');
                $t->text('terms')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'vehicle_id']);
                $t->index(['business_id', 'repair_order_id'], 'vrc_warranty_biz_ro_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_warranties');
        Schema::dropIfExists('vrc_insurance_claims');
        Schema::dropIfExists('vrc_insurance_companies');
    }
};
