<?php

use MintyPHP\Buffer;
use MintyPHP\Module\Helpdesk\HelpdeskAuthorizationPolicy;
use MintyPHP\Module\Helpdesk\Service\HelpdeskSettingsGateway;
use MintyPHP\Support\Guard;

Guard::requireLogin();
Guard::requireAbilityOrForbidden(HelpdeskAuthorizationPolicy::ABILITY_ACCESS);

$request = requestInput();
$query = $request->queryAll();

$filterSchema = require __DIR__ . '/filter-schema.php';
$listFilterContext = gridBuildListFilterContext($filterSchema, [
    'query' => $query,
    'search_keys' => ['search'],
]);
$toolbarFilterSchema = $listFilterContext['toolbarFilterSchema'];
$searchToolbarFilterSchema = $listFilterContext['searchToolbarFilterSchema'];
$drawerToolbarFilterSchema = $listFilterContext['drawerToolbarFilterSchema'];
$toolbarFilterState = $listFilterContext['toolbarFilterState'];
$clientFilterSchema = $listFilterContext['clientFilterSchema'];
$searchConfig = $listFilterContext['searchConfig'];
$schemaByKey = $listFilterContext['schemaByKey'];
$filterChipMeta = [
    'search' => [
        'label' => t('Search'),
        'type' => 'text',
    ],
    'customer' => [
        'label' => t('Customer'),
        'type' => 'text',
    ],
    'contract_type' => [
        'label' => t('Contract type'),
        'type' => 'text',
    ],
    'state' => [
        'label' => t('State'),
        'type' => 'select',
        'default' => (string) ($schemaByKey['state']['default'] ?? 'all'),
        'options' => gridOptionMapFromAllowed((array) ($schemaByKey['state'] ?? [])),
    ],
    'administration' => [
        'label' => t('Administration'),
        'type' => 'select',
        'default' => (string) ($schemaByKey['administration']['default'] ?? 'all'),
        'options' => gridOptionMapFromAllowed((array) ($schemaByKey['administration'] ?? [])),
    ],
    'security_level' => [
        'label' => t('Security level'),
        'type' => 'select',
        'default' => (string) ($schemaByKey['security_level']['default'] ?? 'all'),
        'options' => gridOptionMapFromAllowed((array) ($schemaByKey['security_level'] ?? [])),
    ],
];

$settingsGateway = app(HelpdeskSettingsGateway::class);
$isConfigured = $settingsGateway->isConfigured();

Buffer::set('title', t('Domains'));
Buffer::set('style_groups', json_encode(['helpdesk']));
$breadcrumbs = [
    ['label' => t('Home'), 'path' => 'admin'],
    ['label' => t('Helpdesk'), 'path' => 'helpdesk'],
    ['label' => t('Domains')],
];
