<?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('auction_houses', function (Blueprint $table) {
            $table->id();
            $table->string('name'); // USS Tokyo, TAA Yokohama, CAA Chubu, HAA Kobe, etc.
            $table->string('code')->unique(); // USS-TKY, TAA-YKH, etc.
            $table->string('city'); // Noda, Yokohama, Kobe
            $table->string('country')->default('Japan');
            $table->string('logo_url')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('auction_vehicles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('auction_house_id')->constrained()->cascadeOnDelete();
            $table->string('lot_number')->index(); // 50234
            $table->string('slug')->unique();
            $table->string('chassis_number'); // ZSU60-012938
            $table->string('make'); // Toyota
            $table->string('model'); // Harrier
            $table->string('package')->nullable(); // Premium Advance, Z Leather
            $table->unsignedSmallInteger('year'); // 2021
            $table->unsignedInteger('mileage'); // 28500 km
            $table->unsignedInteger('engine_cc'); // 1980
            $table->string('fuel_type'); // Hybrid, Petrol
            $table->string('transmission')->default('Automatic');
            $table->string('drive_type')->default('2WD');
            $table->string('color');
            $table->string('auction_grade', 10)->default('4.5'); // 5, 4.5, 4, 3.5, R
            $table->string('interior_grade', 5)->default('A');
            $table->string('exterior_grade', 5)->default('A');

            // Bidding & Pricing
            $table->decimal('starting_bid_jpy', 12, 2);
            $table->decimal('expected_bid_jpy', 12, 2)->nullable();
            $table->decimal('current_highest_bid_jpy', 12, 2)->nullable();
            $table->decimal('estimated_bd_cost_bdt', 14, 2); // Calculated landed cost in BDT

            // Auction Timing
            $table->date('auction_date');
            $table->time('auction_time');
            $table->timestamp('auction_starts_at')->nullable();
            $table->timestamp('auction_ends_at')->nullable();

            // Auction Condition & Inspection
            $table->string('main_image');
            $table->json('gallery')->nullable();
            $table->string('auction_sheet_url');
            $table->json('condition_map')->nullable(); // Front bumper A1, Left fender U1, etc.
            $table->text('damage_info')->nullable();
            $table->text('equipment_options')->nullable(); // Sunroof, Navi, Panoramic View, Leather

            // Status & Results
            $table->enum('status', ['upcoming', 'live', 'ended', 'cancelled'])->default('upcoming');
            $table->enum('result_status', ['pending', 'won', 'lost', 'passed', 'withdrawn'])->default('pending');
            $table->decimal('final_hammer_price_jpy', 12, 2)->nullable();
            $table->unsignedInteger('bids_count')->default(0);
            $table->unsignedBigInteger('views_count')->default(0);

            $table->timestamps();

            // Indexes
            $table->index(['auction_date', 'status']);
            $table->index(['make', 'model']);
            $table->index('auction_grade');
            $table->index('starting_bid_jpy');
        });
    }

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