<?php

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

/**
 * Digital Vehicle Inspection (spec §5.4 / §6.5): templates + items, and the
 * per-repair-order inspections with a result per item. Photos attach via the
 * ERP media module (App\Media); signatures store a path via ERP media. All
 * tables prefixed vrc_ and scoped by business_id.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_inspection_templates')) {
            Schema::create('vrc_inspection_templates', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('vehicle_type', 40)->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
            });
        }

        if (! Schema::hasTable('vrc_inspection_template_items')) {
            Schema::create('vrc_inspection_template_items', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('template_id');
                $t->string('section', 120)->nullable();
                $t->string('label', 191);
                $t->string('label_ar', 191)->nullable();
                $t->string('input_type', 20)->default('status'); // status|number|text|photo
                $t->integer('sort_order')->default(0);
                $t->timestamps();
                $t->index(['business_id', 'template_id']);
            });
        }

        if (! Schema::hasTable('vrc_inspections')) {
            Schema::create('vrc_inspections', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('repair_order_id');
                $t->unsignedBigInteger('template_id')->nullable();
                $t->string('type', 20)->default('intake'); // intake|in_process|final
                $t->unsignedInteger('performed_by')->nullable(); // ERP user
                $t->dateTime('performed_at')->nullable();
                $t->text('summary')->nullable();
                $t->string('customer_signature_path', 255)->nullable();
                $t->string('advisor_signature_path', 255)->nullable();
                $t->string('status', 20)->default('draft'); // draft|completed
                $t->timestamps();
                $t->index(['business_id', 'repair_order_id']);
            });
        }

        if (! Schema::hasTable('vrc_inspection_results')) {
            Schema::create('vrc_inspection_results', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('inspection_id');
                $t->unsignedBigInteger('template_item_id')->nullable();
                $t->string('label', 191);
                $t->string('result', 20)->nullable(); // ok|attention|critical|na
                $t->string('value', 191)->nullable();
                $t->text('note')->nullable();
                $t->unsignedBigInteger('recommended_service_id')->nullable(); // vrc_services → push to estimate
                $t->timestamps();
                $t->index(['business_id', 'inspection_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_inspection_results');
        Schema::dropIfExists('vrc_inspections');
        Schema::dropIfExists('vrc_inspection_template_items');
        Schema::dropIfExists('vrc_inspection_templates');
    }
};
