<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::table('car_orders', function (Blueprint $table) {
            // Add quantity and price breakdown columns
            if (!Schema::hasColumn('car_orders', 'quantity')) {
                $table->integer('quantity')->default(1)->after('car_id');
            }
            if (!Schema::hasColumn('car_orders', 'unit_price')) {
                $table->decimal('unit_price', 15, 2)->nullable()->after('quantity');
            }
            if (!Schema::hasColumn('car_orders', 'total_price')) {
                $table->decimal('total_price', 15, 2)->nullable()->after('unit_price');
            }

            // Add payment status
            if (!Schema::hasColumn('car_orders', 'payment_status')) {
                $table->string('payment_status')->default('pending')->after('payment_proof');
            }

            // Add tracking status
            if (!Schema::hasColumn('car_orders', 'tracking_status')) {
                $table->string('tracking_status')->nullable()->after('tracking_number');
            }

            // Add billing detail columns
            if (!Schema::hasColumn('car_orders', 'billing_name')) {
                $table->string('billing_name')->nullable()->after('shipping_address');
            }
            if (!Schema::hasColumn('car_orders', 'billing_email')) {
                $table->string('billing_email')->nullable()->after('billing_name');
            }
            if (!Schema::hasColumn('car_orders', 'billing_phone')) {
                $table->string('billing_phone')->nullable()->after('billing_email');
            }
            if (!Schema::hasColumn('car_orders', 'billing_company')) {
                $table->string('billing_company')->nullable()->after('billing_phone');
            }
            if (!Schema::hasColumn('car_orders', 'billing_address')) {
                $table->text('billing_address')->nullable()->after('billing_company');
            }
            if (!Schema::hasColumn('car_orders', 'billing_city')) {
                $table->string('billing_city')->nullable()->after('billing_address');
            }
            if (!Schema::hasColumn('car_orders', 'billing_state')) {
                $table->string('billing_state')->nullable()->after('billing_city');
            }
            if (!Schema::hasColumn('car_orders', 'billing_postal_code')) {
                $table->string('billing_postal_code')->nullable()->after('billing_state');
            }
            if (!Schema::hasColumn('car_orders', 'billing_country')) {
                $table->string('billing_country')->nullable()->after('billing_postal_code');
            }

            // Add admin_notes (keep existing admin_note too)
            if (!Schema::hasColumn('car_orders', 'admin_notes')) {
                $table->text('admin_notes')->nullable()->after('user_note');
            }

            // Add cancelled_at and completed_at timestamps
            if (!Schema::hasColumn('car_orders', 'cancelled_at')) {
                $table->timestamp('cancelled_at')->nullable()->after('admin_notes');
            }
            if (!Schema::hasColumn('car_orders', 'completed_at')) {
                $table->timestamp('completed_at')->nullable()->after('cancelled_at');
            }

            // Add soft deletes
            if (!Schema::hasColumn('car_orders', 'deleted_at')) {
                $table->softDeletes();
            }
        });
    }

    public function down()
    {
        Schema::table('car_orders', function (Blueprint $table) {
            $columns = [
                'quantity', 'unit_price', 'total_price', 'payment_status',
                'tracking_status', 'billing_name', 'billing_email', 'billing_phone',
                'billing_company', 'billing_address', 'billing_city', 'billing_state',
                'billing_postal_code', 'billing_country', 'admin_notes',
                'cancelled_at', 'completed_at', 'deleted_at',
            ];

            $toDrop = array_filter($columns, fn($col) => Schema::hasColumn('car_orders', $col));
            if (!empty($toDrop)) {
                $table->dropColumn($toDrop);
            }
        });
    }
};
