<?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('vehicles', function (Blueprint $table) {
            $table->id();
            $table->string('stock_number')->unique(); // MS-STK-2024-001
            $table->string('slug')->unique();
            $table->string('make'); // Toyota, Honda, Nissan, Mazda, etc.
            $table->string('model'); // Harrier, Vezel, Prado, etc.
            $table->string('variant')->nullable(); // Z Leather, RS Sensing, TX L Package
            $table->unsignedSmallInteger('year');
            $table->unsignedSmallInteger('registration_year')->nullable();
            $table->string('chassis_number')->unique();
            $table->string('engine_number')->nullable();
            $table->unsignedInteger('engine_cc');
            $table->string('fuel_type'); // Hybrid, Petrol, Diesel, EV, Plug-in Hybrid
            $table->string('transmission')->default('Automatic'); // Automatic, CVT, e-CVT, Manual
            $table->string('drive_type')->default('2WD'); // 2WD, 4WD, AWD
            $table->string('body_type'); // SUV, Sedan, MPV, Hatchback, Cross, Van
            $table->string('color');
            $table->unsignedInteger('mileage'); // in km
            $table->string('auction_grade', 10)->default('4.5'); // S, 6, 5, 4.5, 4, 3.5, R
            $table->string('interior_grade', 5)->default('A'); // A, B, C, D
            $table->string('exterior_grade', 5)->default('A'); // A, B, C, D

            // Pricing
            $table->decimal('purchase_price_jpy', 12, 2)->nullable();
            $table->decimal('reconditioning_cost_bdt', 12, 2)->default(0);
            $table->decimal('customs_duty_bdt', 12, 2)->default(0);
            $table->decimal('registration_cost_bdt', 12, 2)->default(0);
            $table->decimal('other_charges_bdt', 12, 2)->default(0);
            $table->decimal('selling_price_bdt', 14, 2);
            $table->decimal('discount_bdt', 12, 2)->default(0);
            $table->decimal('final_price_bdt', 14, 2);

            // Media
            $table->string('main_image');
            $table->json('gallery')->nullable();
            $table->json('interior_photos')->nullable();
            $table->json('exterior_photos')->nullable();
            $table->json('engine_photos')->nullable();
            $table->json('tyre_photos')->nullable();
            $table->string('auction_sheet_url')->nullable();
            $table->string('inspection_report_url')->nullable();
            $table->string('video_url')->nullable();

            // Status & Flags
            $table->enum('status', ['available', 'reserved', 'sold', 'arriving', 'hidden'])->default('available');
            $table->boolean('is_featured')->default(false);
            $table->boolean('is_new_arrival')->default(true);
            $table->text('description')->nullable();
            $table->json('features_list')->nullable(); // Push start, Leather seats, Sunroof, 360 camera, etc.
            $table->unsignedBigInteger('views_count')->default(0);

            $table->timestamps();

            // Indexes for fast searching & filtering
            $table->index(['make', 'model']);
            $table->index('year');
            $table->index('final_price_bdt');
            $table->index('status');
            $table->index('is_featured');
        });
    }

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