<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Create enum type only if it doesn't exist
        DB::statement("
            DO $$ BEGIN
                CREATE TYPE user_status_enum AS ENUM (
                    'active',
                    'inactive',
                    'pending'
                );
            EXCEPTION
                WHEN duplicate_object THEN null;
            END $$;
        ");

        // Add column if it doesn't exist
        DB::statement("
            ALTER TABLE users
            ADD COLUMN IF NOT EXISTS status user_status_enum
            DEFAULT 'inactive'
            NOT NULL
        ");
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('status');
        });

        DB::statement("DROP TYPE IF EXISTS user_status_enum");
    }
};