<?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('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_number')->unique(); // MS-ORD-2024-0012
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('vehicle_id')->nullable()->constrained('vehicles')->nullOnDelete();
            $table->foreignId('auction_vehicle_id')->nullable()->constrained('auction_vehicles')->nullOnDelete();
            $table->foreignId('bid_id')->nullable()->constrained('bids')->nullOnDelete();

            $table->enum('order_type', ['auction_preorder', 'ready_stock'])->default('auction_preorder');

            // Financial Summary
            $table->decimal('total_amount_bdt', 14, 2);
            $table->decimal('paid_amount_bdt', 14, 2)->default(0);
            $table->decimal('due_amount_bdt', 14, 2);
            $table->decimal('hammer_price_jpy', 12, 2)->nullable();

            // Import Tracking Status Pipeline
            $table->enum('status', [
                'auction_won',
                'payment_pending',
                'payment_received',
                'japan_processing',
                'preparing_shipment',
                'shipped',
                'arrived_chittagong',
                'customs_processing',
                'customs_cleared',
                'registration',
                'ready_for_delivery',
                'delivered',
                'cancelled'
            ])->default('auction_won');

            // Shipping & Logistics Information
            $table->string('vessel_name')->nullable(); // e.g. "TRANS HARMONY 1 V.042"
            $table->string('voyage_number')->nullable();
            $table->string('bl_number')->nullable(); // Bill of Lading e.g. "ECL-TYO-CTG-89410"
            $table->string('container_number')->nullable();
            $table->string('shipping_line')->nullable(); // Eastern Car Liner (ECL), Toyofuji
            $table->string('port_of_loading')->default('Yokohama, Japan');
            $table->string('port_of_discharge')->default('Chittagong Port, Bangladesh');
            $table->date('etd_japan')->nullable(); // Estimated Time of Departure
            $table->date('eta_chittagong')->nullable(); // Estimated Time of Arrival
            $table->date('customs_cleared_date')->nullable();
            $table->date('registration_date')->nullable();
            $table->date('delivery_date')->nullable();

            // Registration & Final Details
            $table->string('assigned_reg_number')->nullable(); // Dhaka Metro-Gha-XX-XXXX
            $table->text('delivery_address')->nullable();
            $table->text('internal_notes')->nullable();
            $table->timestamps();

            // Indexes
            $table->index(['user_id', 'status']);
            $table->index('status');
        });

        Schema::create('order_status_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
            $table->string('status');
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('location')->nullable(); // "Yokohama Port", "Chittagong Port Yard 4"
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('occurred_at')->useCurrent();
            $table->timestamps();
        });
    }

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