<?php

use MintyPHP\Service\Access\PermissionAuthorizationPolicy;
use MintyPHP\Support\Guard;

Guard::requireLogin();
gridRequireGetRequest();

Guard::requireAbilityOrForbidden(PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_VIEW);

$filters = gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php');

$result = app(\MintyPHP\Service\Access\PermissionService::class)->listPaged([
    'limit' => $filters['limit'],
    'offset' => $filters['offset'],
    'search' => $filters['search'],
    'order' => $filters['order'],
    'dir' => $filters['dir'],
    'active' => $filters['active'],
]);

$rows = [];
foreach ($result['rows'] ?? [] as $row) {
    $rows[] = [
        'id' => $row['id'] ?? null,
        'key' => $row['key'] ?? '',
        'description' => $row['description'] ?? '',
        'active' => (int) ($row['active'] ?? 1),
        'is_system' => (int) ($row['is_system'] ?? 0),
        'created' => dt($row['created'] ?? ''),
    ];
}

gridJsonDataResult($rows, (int) ($result['total'] ?? 0));
