<?php

use MintyPHP\Module\Audit\Domain\SystemAuditChannel;
use MintyPHP\Module\Audit\Domain\SystemAuditOutcome;

$outcomeItems = array_map(
    static fn (SystemAuditOutcome $outcome): array => [
        'id' => $outcome->value,
        'description' => $outcome->labelToken(),
    ],
    SystemAuditOutcome::cases()
);
$channelItems = array_map(
    static fn (SystemAuditChannel $channel): array => [
        'id' => $channel->value,
        'description' => $channel->labelToken(),
        'translate' => false,
    ],
    SystemAuditChannel::cases()
);

return gridFilterSchema([
    'query' => [
        'limit' => ['type' => 'int', 'default' => 20, 'min' => 1, 'max' => 200],
        'offset' => ['type' => 'int', 'default' => 0, 'min' => 0],
        'search' => ['type' => 'string'],
        'event_types' => [
            'type' => 'csv_strings',
            'max' => 200,
            'sanitizer' => static function (string $value): string {
                $value = strtolower(trim($value));
                if ($value === '' || strlen($value) > 64) {
                    return '';
                }
                return preg_match('/^[a-z0-9._-]+$/', $value) === 1 ? $value : '';
            },
        ],
        'outcome' => ['type' => 'enum', 'allowed' => SystemAuditOutcome::values(), 'default' => '', 'lowercase' => true],
        'channel' => ['type' => 'enum', 'allowed' => SystemAuditChannel::values(), 'default' => '', 'lowercase' => true],
        'created_from' => ['type' => 'date'],
        'created_to' => ['type' => 'date'],
        'actor_user_ids' => ['type' => 'csv_ids', 'max' => 200],
        'target_type' => ['type' => 'string'],
        'request_id' => ['type' => 'string'],
        'order' => ['type' => 'order', 'allowed' => ['id', 'created_at', 'event_type', 'outcome', 'channel', 'actor_user_id'], 'default' => 'created_at'],
        'dir' => ['type' => 'dir', 'default' => 'desc'],
    ],
    'toolbar' => [
        [
            // Dormant capability — kept in toolbar as hidden so Grid.js preserves
            // the param across refetches if/when a UI is added.
            'key' => 'target_type',
            'type' => 'hidden',
            'input_id' => 'system-audit-target-type-filter',
            'default' => '',
        ],
        [
            'key' => 'search',
            'type' => 'text',
            'label' => 'Search',
            'placeholder' => 'Search...',
            'input_id' => 'system-audit-search',
            'search' => true,
            'query' => ['type' => 'string'],
        ],
        [
            'key' => 'outcome',
            'type' => 'select',
            'label' => 'Status',
            'input_id' => 'system-audit-outcome-filter',
            'default' => '',
            'allowed' => [
                ['id' => '', 'description' => 'All outcomes'],
                ...$outcomeItems,
            ],
        ],
        [
            'key' => 'channel',
            'type' => 'select',
            'label' => 'Channel',
            'input_id' => 'system-audit-channel-filter',
            'default' => '',
            'allowed' => [
                ['id' => '', 'description' => 'All channels'],
                ...$channelItems,
            ],
        ],
        [
            'key' => 'event_types',
            'type' => 'multi_csv',
            'label' => 'Event type',
            'placeholder' => 'Select event types',
            'input_id' => 'system-audit-event-types-filter',
            'options_key' => 'event_type_items',
            'default' => [],
            'query' => ['type' => 'csv_strings', 'max' => 200, 'return' => 'array'],
        ],
        [
            'key' => 'created_from',
            'type' => 'date',
            'label' => 'Created from',
            'input_id' => 'system-audit-created-from',
        ],
        [
            'key' => 'created_to',
            'type' => 'date',
            'label' => 'Created to',
            'input_id' => 'system-audit-created-to',
        ],
        [
            'key' => 'actor_user_ids',
            'type' => 'multi_csv',
            'label' => 'Actor',
            'placeholder' => 'Select actor users',
            'input_id' => 'system-audit-actor-users-filter',
            'options_key' => 'actor_user_items',
            'default' => [],
            'query' => ['type' => 'csv_ids', 'max' => 200, 'return' => 'array'],
        ],
        [
            'key' => 'request_id',
            'type' => 'text',
            'label' => 'Request ID',
            'input_id' => 'system-audit-request-id',
            'normalize' => 'trim_lower',
            'event' => 'input',
        ],
    ],
]);
