<?php

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

/**
 * Phase 5 — ISO 18541 Repair & Maintenance Information (RMI) access log. Records
 * each standardized RMI/VIN lookup (provider, use-case, query, response) so the
 * technician's reference data is auditable. The actual data source is a pluggable
 * RepairInfoServiceContract adapter (stub by default; swap for a commercial
 * provider such as TecRMI/HaynesPro/Autodata via config).
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_rmi_lookups')) {
            Schema::create('vrc_rmi_lookups', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('vehicle_id')->nullable();
                $t->unsignedBigInteger('repair_order_id')->nullable();
                $t->string('provider', 60)->nullable();
                $t->string('vin', 40)->nullable();
                $t->string('use_case', 30)->nullable(); // repair|maintenance|diagnostics|wiring|tsb|recall|vin_decode
                $t->string('query', 255)->nullable();
                $t->longText('response')->nullable();
                $t->string('status', 20)->default('ok'); // ok|not_found|error
                $t->unsignedInteger('created_by')->nullable();
                $t->timestamp('created_at')->nullable();
                $t->index(['business_id', 'vehicle_id'], 'vrc_rmi_biz_vehicle_idx');
            });
        }
    }

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