<?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.
     */
    public function up(): void
    {
        // 1. Rename the profile table (if it still exists)
        if (Schema::hasTable('customer_profiles')) {
            Schema::rename('customer_profiles', 'employee_profiles');
        }

        // 2. Add 'employee' to the enum (keep 'customer' temporarily)
        if (DB::getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE users MODIFY COLUMN type ENUM('super-admin', 'admin', 'customer', 'employee') NOT NULL DEFAULT 'customer'");
        } elseif (DB::getDriverName() === 'sqlite') {
            // SQLite doesn't support MODIFY COLUMN for enums easily, we'll use change() to string to relax constraint
            Schema::table('users', function (Blueprint $table) {
                $table->string('type')->default('employee')->change();
            });
        }

        // 3. Update existing 'customer' types to 'employee'
        DB::table('users')->where('type', 'customer')->update(['type' => 'employee']);

        // 4. Finalize enum by removing 'customer'
        if (DB::getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE users MODIFY COLUMN type ENUM('super-admin', 'admin', 'employee') NOT NULL DEFAULT 'employee'");
        }

    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // 1. Revert user type column to include 'customer'
        if (DB::getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE users MODIFY COLUMN type ENUM('super-admin', 'admin', 'customer') NOT NULL DEFAULT 'customer'");
        }

        // 2. Revert 'employee' types to 'customer'
        DB::table('users')->where('type', 'employee')->update(['type' => 'customer']);

        // 3. Rename the profile table back
        Schema::rename('employee_profiles', 'customer_profiles');
    }
};
