<?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 active column to bookings if missing
        if (Schema::hasTable('bookings') && ! Schema::hasColumn('bookings', 'active')) {
            Schema::table('bookings', function (Blueprint $table): void {
                $table->unsignedTinyInteger('active')->default(1)->after('description');
                $table->index('active');
            });
        }

        // Add active column to activities if missing
        if (Schema::hasTable('activities') && ! Schema::hasColumn('activities', 'active')) {
            Schema::table('activities', function (Blueprint $table): void {
                $table->unsignedTinyInteger('active')->default(1)->after('note');
                $table->index('active');
            });
        }

        // Add active column to documents if missing
        if (Schema::hasTable('documents') && ! Schema::hasColumn('documents', 'active')) {
            Schema::table('documents', function (Blueprint $table): void {
                $table->unsignedTinyInteger('active')->default(1)->after('is_public');
                $table->index('active');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasTable('bookings') && Schema::hasColumn('bookings', 'active')) {
            Schema::table('bookings', function (Blueprint $table): void {
                $table->dropIndex(['active']);
                $table->dropColumn('active');
            });
        }
        if (Schema::hasTable('activities') && Schema::hasColumn('activities', 'active')) {
            Schema::table('activities', function (Blueprint $table): void {
                $table->dropIndex(['active']);
                $table->dropColumn('active');
            });
        }
        if (Schema::hasTable('documents') && Schema::hasColumn('documents', 'active')) {
            Schema::table('documents', function (Blueprint $table): void {
                $table->dropIndex(['active']);
                $table->dropColumn('active');
            });
        }
    }
};
