<?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::table('orders', function (Blueprint $table) {
            // Update existing UUID user_id to be properly constrained
            if (Schema::hasColumn('orders', 'user_id')) {
                // Drop existing UUID user_id and recreate as integer
                $table->dropColumn('user_id');
            }
        });

        Schema::table('orders', function (Blueprint $table) {
            // Add user_id as UUID to match users table
            if (!Schema::hasColumn('orders', 'user_id')) {
                $table->uuid('user_id')->after('id');
            }

            if (!Schema::hasColumn('orders', 'order_number')) {
                $table->string('order_number')->unique()->after('vendor_id');
            }

            if (!Schema::hasColumn('orders', 'marketplace_status')) {
                $table->enum('marketplace_status', ['pending', 'confirmed', 'in_production', 'shipped', 'delivered', 'cancelled', 'refunded'])->default('pending')->after('order_number');
            }

            if (!Schema::hasColumn('orders', 'subtotal')) {
                $table->decimal('subtotal', 10, 2)->default(0)->after('marketplace_status');
            }

            if (!Schema::hasColumn('orders', 'tax_amount')) {
                $table->decimal('tax_amount', 10, 2)->default(0)->after('subtotal');
            }

            if (!Schema::hasColumn('orders', 'shipping_cost')) {
                $table->decimal('shipping_cost', 10, 2)->default(0)->after('tax_amount');
            }

            if (!Schema::hasColumn('orders', 'currency')) {
                $table->string('currency', 3)->default('USD')->after('total_amount');
            }

            if (!Schema::hasColumn('orders', 'customer_notes')) {
                $table->text('customer_notes')->nullable()->after('currency');
            }

            if (!Schema::hasColumn('orders', 'vendor_notes')) {
                $table->text('vendor_notes')->nullable()->after('customer_notes');
            }

            if (!Schema::hasColumn('orders', 'required_date')) {
                $table->date('required_date')->nullable()->after('vendor_notes');
            }

            if (!Schema::hasColumn('orders', 'estimated_delivery_date')) {
                $table->date('estimated_delivery_date')->nullable()->after('required_date');
            }

            if (!Schema::hasColumn('orders', 'actual_delivery_date')) {
                $table->date('actual_delivery_date')->nullable()->after('estimated_delivery_date');
            }

            if (!Schema::hasColumn('orders', 'tracking_number')) {
                $table->string('tracking_number')->nullable()->after('actual_delivery_date');
            }

            if (!Schema::hasColumn('orders', 'shipping_address')) {
                $table->json('shipping_address')->nullable()->after('tracking_number');
            }

            if (!Schema::hasColumn('orders', 'billing_address')) {
                $table->json('billing_address')->nullable()->after('shipping_address');
            }

            if (!Schema::hasColumn('orders', 'deleted_at')) {
                $table->softDeletes();
            }
        });

        // Note: Foreign keys will be added in a separate migration after all tables are properly updated to UUID
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $columns = [
                'user_id',
                'order_number',
                'marketplace_status',
                'subtotal',
                'tax_amount',
                'shipping_cost',
                'total_amount',
                'currency',
                'customer_notes',
                'vendor_notes',
                'required_date',
                'estimated_delivery_date',
                'actual_delivery_date',
                'tracking_number',
                'shipping_address',
                'billing_address',
                'deleted_at'
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('orders', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
