<?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
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('staff_role', 20)->nullable()->after('role');
        });

        DB::table('users')
            ->whereIn('role', ['SALES', 'OPS'])
            ->update([
                'staff_role' => DB::raw('role'),
                'role' => 'STAFF',
            ]);

        DB::table('users')
            ->whereNotIn('role', ['ADMIN', 'STAFF'])
            ->update([
                'role' => 'STAFF',
            ]);
    }

    public function down(): void
    {
        DB::table('users')
            ->where('role', 'STAFF')
            ->whereNotNull('staff_role')
            ->update([
                'role' => DB::raw('staff_role'),
            ]);

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