<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Ganti kolom boolean `active` dengan enum `status`:
     * active | suspended | deactivated.
     */
    public function up(): void
    {
        // Tambah kolom status, isi dari active lama (true→active, false→deactivated).
        Schema::table('users', function (Blueprint $table) {
            $table->enum('status', ['active', 'suspended', 'deactivated'])->default('active')->after('credit');
        });

        DB::table('users')->where('active', true)->update(['status' => 'active']);
        DB::table('users')->where('active', false)->update(['status' => 'deactivated']);

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('active');
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->boolean('active')->default(true)->after('credit');
        });

        DB::table('users')->where('status', 'active')->update(['active' => true]);
        DB::table('users')->where('status', '!=', 'active')->update(['active' => false]);

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