<?php

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

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

$id = (int) ($id ?? 0);
if ($id <= 0) {
    Router::redirect('admin/permissions');
}

$result = permissionGateway()->deleteById($id);
if (!($result['ok'] ?? false)) {
    $error = (string) ($result['error'] ?? '');
    if ($error === 'system_permission_protected') {
        Flash::error('System permission can not be deleted', 'admin/permissions', 'permission_system_protected');
    } else {
        Flash::error('Permission not found', 'admin/permissions', 'permission_not_found');
    }
    Router::redirect('admin/permissions');
}

Flash::success('Permission deleted', 'admin/permissions', 'permission_deleted');
Router::redirect('admin/permissions');
