<?php

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

/**
 * Vehicles & catalog (spec §5.1): vehicle makes, models and the vehicle record
 * itself — attached to an ERP customer by erp_customer_id (no FK; matches the
 * module convention). All tables prefixed vrc_ and scoped by business_id.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_vehicle_makes')) {
            Schema::create('vrc_vehicle_makes', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('logo_path', 255)->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active']);
            });
        }

        if (! Schema::hasTable('vrc_vehicle_models')) {
            Schema::create('vrc_vehicle_models', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('make_id');
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'make_id']);
                $t->unique(['make_id', 'name']);
            });
        }

        if (! Schema::hasTable('vrc_vehicles')) {
            Schema::create('vrc_vehicles', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('erp_customer_id');           // App\Contact id (no FK)
                $t->unsignedBigInteger('make_id')->nullable();
                $t->unsignedBigInteger('model_id')->nullable();
                $t->unsignedSmallInteger('year')->nullable();
                $t->string('plate_number', 30)->nullable();
                $t->string('vin', 40)->nullable();
                $t->string('engine_number', 60)->nullable();
                $t->string('color', 40)->nullable();
                $t->string('transmission', 20)->nullable();       // manual|automatic|cvt|...
                $t->string('fuel_type', 20)->nullable();          // petrol|diesel|hybrid|electric|...
                $t->unsignedInteger('odometer')->nullable();
                $t->string('odometer_unit', 5)->default('km');    // km|mi
                $t->string('vehicle_type', 40)->nullable();
                $t->date('registration_expiry')->nullable();
                $t->unsignedBigInteger('insurance_company_id')->nullable();
                $t->string('insurance_policy_no', 80)->nullable();
                $t->date('insurance_expiry')->nullable();
                $t->text('notes')->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->softDeletes();
                $t->index(['business_id', 'erp_customer_id']);
                $t->index(['business_id', 'plate_number']);
                $t->index(['business_id', 'vin']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_vehicles');
        Schema::dropIfExists('vrc_vehicle_models');
        Schema::dropIfExists('vrc_vehicle_makes');
    }
};
