<?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 hotel_id to menu_categories
        if (!Schema::hasColumn('menu_categories', 'hotel_id')) {
            Schema::table('menu_categories', function (Blueprint $table) {
                $table->unsignedBigInteger('hotel_id')->after('id')->nullable();
                $table->index('hotel_id');
            });
        }

        // Add hotel_id to menu_items
        if (!Schema::hasColumn('menu_items', 'hotel_id')) {
            Schema::table('menu_items', function (Blueprint $table) {
                $table->unsignedBigInteger('hotel_id')->after('id')->nullable();
                $table->index('hotel_id');
            });
        }

        // Add hotel_id to order_items (mostly for consistency with orders)
        if (!Schema::hasColumn('order_items', 'hotel_id')) {
            Schema::table('order_items', function (Blueprint $table) {
                $table->unsignedBigInteger('hotel_id')->after('id')->nullable();
                $table->index('hotel_id');
            });
        }
        
        // Ensure hotel_id is indexed in orders (if it already exists)
        if (Schema::hasColumn('orders', 'hotel_id')) {
            Schema::table('orders', function (Blueprint $table) {
                // Check if index exists - skip if yes to avoid error
                // In SQLite/MySQL, adding an index is usually fine even if it exists but let's be safe
                //$table->index('hotel_id');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('menu_categories', function (Blueprint $table) {
            $table->dropColumn('hotel_id');
        });
        Schema::table('menu_items', function (Blueprint $table) {
            $table->dropColumn('hotel_id');
        });
        Schema::table('order_items', function (Blueprint $table) {
            $table->dropColumn('hotel_id');
        });
    }
};
