<?php

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

/**
 * Phase 1 (§4.4) — record the final road-test outcome on the delivery DVI.
 * The final inspection already gates pickup; these columns capture *whether* a
 * test drive was done, its pass/fail result, the odometer at test, and notes —
 * so the QC sign-off is auditable, not just implied.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_inspections')) {
            return;
        }
        Schema::table('vrc_inspections', function (Blueprint $t) {
            if (! Schema::hasColumn('vrc_inspections', 'test_drive_done')) {
                $t->boolean('test_drive_done')->default(false)->after('body_diagram');
            }
            if (! Schema::hasColumn('vrc_inspections', 'test_drive_result')) {
                $t->string('test_drive_result', 20)->nullable()->after('test_drive_done');
            }
            if (! Schema::hasColumn('vrc_inspections', 'test_drive_odometer')) {
                $t->unsignedInteger('test_drive_odometer')->nullable()->after('test_drive_result');
            }
            if (! Schema::hasColumn('vrc_inspections', 'test_drive_notes')) {
                $t->string('test_drive_notes', 500)->nullable()->after('test_drive_odometer');
            }
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('vrc_inspections')) {
            return;
        }
        Schema::table('vrc_inspections', function (Blueprint $t) {
            foreach (['test_drive_done', 'test_drive_result', 'test_drive_odometer', 'test_drive_notes'] as $col) {
                if (Schema::hasColumn('vrc_inspections', $col)) {
                    $t->dropColumn($col);
                }
            }
        });
    }
};
