<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Add columns one by one, checking if they exist first
        // This makes the migration safe for both local (where columns might exist) and live (where they don't)
        
        if (!Schema::hasColumn('pos_order_updates', 'order_id')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->foreignId('order_id')->after('id')->constrained('pos_orders')->onDelete('cascade');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'order_item_id')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->foreignId('order_item_id')->nullable()->after('order_id')->constrained('pos_order_items')->nullOnDelete();
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'menu_item_id')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->foreignId('menu_item_id')->after('order_item_id')->constrained('pos_menu_items')->onDelete('cascade');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'item_name')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->string('item_name')->after('menu_item_id');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'change_type')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->string('change_type')->after('item_name');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'quantity')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->decimal('quantity', 10, 2)->default(0)->after('change_type');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'original_quantity')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->decimal('original_quantity', 10, 2)->nullable()->after('quantity');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'new_quantity')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->decimal('new_quantity', 10, 2)->nullable()->after('original_quantity');
            });
        }
        
        if (!Schema::hasColumn('pos_order_updates', 'updated_by')) {
            Schema::table('pos_order_updates', function (Blueprint $table) {
                $table->foreignId('updated_by')->after('new_quantity')->constrained('users')->onDelete('cascade');
            });
        }
        
        // Add indexes separately
        // Note: Foreign keys (order_id, order_item_id, menu_item_id, updated_by) automatically create indexes
        // So we only need to add indexes for non-foreign-key columns
        
        // Add change_type index (if column exists and index doesn't exist)
        if (Schema::hasColumn('pos_order_updates', 'change_type')) {
            try {
                Schema::table('pos_order_updates', function (Blueprint $table) {
                    $table->index('change_type');
                });
            } catch (\Illuminate\Database\QueryException $e) {
                // Index might already exist, ignore duplicate key errors
                if (strpos($e->getMessage(), 'Duplicate key name') === false && 
                    strpos($e->getMessage(), '1061') === false) {
                    throw $e;
                }
            }
        }
        
        // Add created_at index (if column exists and index doesn't exist)
        if (Schema::hasColumn('pos_order_updates', 'created_at')) {
            try {
                Schema::table('pos_order_updates', function (Blueprint $table) {
                    $table->index('created_at');
                });
            } catch (\Illuminate\Database\QueryException $e) {
                // Index might already exist, ignore duplicate key errors
                if (strpos($e->getMessage(), 'Duplicate key name') === false && 
                    strpos($e->getMessage(), '1061') === false) {
                    throw $e;
                }
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('pos_order_updates', function (Blueprint $table) {
            // Drop indexes first
            $table->dropIndex(['order_id']);
            $table->dropIndex(['change_type']);
            $table->dropIndex(['created_at']);
            
            // Drop foreign keys and columns
            $table->dropForeign(['order_id']);
            $table->dropForeign(['order_item_id']);
            $table->dropForeign(['menu_item_id']);
            $table->dropForeign(['updated_by']);
            
            $table->dropColumn([
                'order_id',
                'order_item_id',
                'menu_item_id',
                'item_name',
                'change_type',
                'quantity',
                'original_quantity',
                'new_quantity',
                'updated_by'
            ]);
        });
    }
};
