<?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
    {
        $tables = [
            'audit_logs',
            'pending_staff_requests'
        ];

        foreach ($tables as $tableName) {
            if (Schema::hasTable($tableName)) {
                Schema::table($tableName, function (Blueprint $table) use ($tableName) {
                    if (!Schema::hasColumn($tableName, 'hotel_id')) {
                        $table->unsignedBigInteger('hotel_id')->nullable()->after('id')->index();
                        // Add foreign key if hotels table exists and it's a fresh install
                        if (Schema::hasTable('hotels')) {
                            // $table->foreign('hotel_id')->references('id')->on('hotels')->onDelete('cascade');
                        }
                    }
                });
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $tables = [
            'audit_logs',
            'pending_staff_requests'
        ];

        foreach ($tables as $tableName) {
            if (Schema::hasTable($tableName)) {
                Schema::table($tableName, function (Blueprint $table) use ($tableName) {
                    if (Schema::hasColumn($tableName, 'hotel_id')) {
                        $table->dropColumn('hotel_id');
                    }
                });
            }
        }
    }
};
