<?php

use App\Domains\Authorization\Enums\RoleCode;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    private const REMOVED_ROLE_CODE = 'tenant_admin';

    public function up(): void
    {
        DB::table('users')
            ->where('rol_codigo', self::REMOVED_ROLE_CODE)
            ->update([
                'rol_codigo' => RoleCode::User->value,
                'tenant_codigo' => null,
            ]);

        DB::table('roles')
            ->where('codigo', self::REMOVED_ROLE_CODE)
            ->delete();
    }

    public function down(): void
    {
        DB::table('roles')->insertOrIgnore([
            'codigo' => self::REMOVED_ROLE_CODE,
            'nombre' => 'Administrador del tenant',
            'descripcion' => 'Administra la operación de su propio tenant.',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
};
