<?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('bodyshop_jobs', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('work_order_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('vehicle_id')->nullable()->constrained()->nullOnDelete();

            $t->string('number', 32);
            $t->string('type', 24)->default('body_repair'); // body_repair/pdr/painting/detailing/ceramic/ppf/polishing
            $t->string('status', 16)->default('estimate');  // estimate/approved/in_progress/done/delivered/cancelled

            // Insurance case
            $t->boolean('is_insurance')->default(false);
            $t->string('insurer')->nullable();
            $t->string('policy_no', 64)->nullable();
            $t->string('claim_no', 64)->nullable();
            $t->string('insurance_status', 24)->nullable(); // submitted/approved/rejected/paid

            $t->decimal('estimate_amount', 12, 2)->default(0);
            $t->decimal('approved_amount', 12, 2)->default(0);
            $t->text('notes')->nullable();

            $t->timestamps();
            $t->softDeletes();

            $t->unique(['company_id', 'number']);
            $t->index(['company_id', 'status']);
            $t->index(['company_id', 'type']);
        });

        Schema::create('damage_points', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('bodyshop_job_id')->constrained()->cascadeOnDelete();
            $t->string('zone', 40);        // Capotă / Aripă FS / Ușă SD / ...
            $t->string('kind', 32);        // Zgârietură / Lovitură / Fisură / Rugină / ...
            $t->string('severity', 12)->default('minor'); // minor/medium/severe
            $t->text('notes')->nullable();
            $t->timestamps();

            $t->index(['company_id', 'bodyshop_job_id']);
        });
    }

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