<?php

use App\Enums\ProductStatus;
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
{
    public function up(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->string('status', 20)->default(ProductStatus::ACTIVE->value)->after('description');
        });

        DB::table('products')->where('is_active', true)->update(['status' => ProductStatus::ACTIVE->value]);
        DB::table('products')->where('is_active', false)->update(['status' => ProductStatus::INACTIVE->value]);

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

    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->boolean('is_active')->default(true)->after('description');
        });

        DB::table('products')->where('status', ProductStatus::ACTIVE->value)->update(['is_active' => true]);
        DB::table('products')->whereIn('status', [ProductStatus::INACTIVE->value, ProductStatus::DRAFT->value])->update(['is_active' => false]);

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