<?php

use MintyPHP\Router;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

Guard::requireLogin();
Guard::requirePermission(PermissionService::ROLES_DELETE);

$uuid = trim((string) ($id ?? ''));
if ($uuid === '') {
    Router::redirect('admin/roles');
}

$result = directoryServicesFactory()->createRoleService()->deleteByUuid($uuid);
if (!($result['ok'] ?? false)) {
    $error = $result['error'] ?? 'unknown';
    if ($error === 'admin_role_protected') {
        Flash::error('The Admin role cannot be deleted', 'admin/roles', 'admin_role_protected');
    } else {
        Flash::error('Role not found', 'admin/roles', 'role_not_found');
    }
    Router::redirect('admin/roles');
}

Flash::success('Role deleted', 'admin/roles', 'role_deleted');
Router::redirect('admin/roles');
