<?php
namespace admin\Support;

class TableListRequestFactory
{
    public const DEFAULT_PER_PAGE_OPTIONS = [5, 10, 15, 25, 50, 100];
    public const DEFAULT_PER_PAGE = 15;

    /**
     * Buduje kontekst listy (filtry, sortowanie, paginacja) z requestu.
     *
     * @return array{
     *   page:int,
     *   perPage:int,
     *   perPageOptions:array<int,int>,
     *   filters:array<string,string>,
     *   viewFilters:array<int,array<string,mixed>>,
     *   queryFilters:array<string,string>,
     *   sortColumn:string,
     *   sortDir:string
     * }
     */
    public static function fromRequest(
        array $filterDefinitions,
        array $sortableColumns,
        string $defaultSortColumn = 'date_add',
        ?array $perPageOptions = null,
        ?int $defaultPerPage = null
    ): array {
        if ($perPageOptions === null) {
            $perPageOptions = self::DEFAULT_PER_PAGE_OPTIONS;
        }

        if ($defaultPerPage === null) {
            $defaultPerPage = self::DEFAULT_PER_PAGE;
        }

        if (!in_array($defaultPerPage, $perPageOptions, true)) {
            $defaultPerPage = (int)$perPageOptions[0];
        }

        $page = max(1, (int)\Shared\Helpers\Helpers::get('page'));
        $perPage = (int)\Shared\Helpers\Helpers::get('per_page');
        if (!in_array($perPage, $perPageOptions, true)) {
            $perPage = $defaultPerPage;
        }

        $filters = [];
        $viewFilters = [];
        $queryFilters = [];

        foreach ($filterDefinitions as $definition) {
            $key = (string)($definition['key'] ?? '');
            if ($key === '') {
                continue;
            }

            $type = (string)($definition['type'] ?? 'text');
            $value = (string)\Shared\Helpers\Helpers::get($key);

            $filters[$key] = $value;
            $queryFilters[$key] = $value;

            $filterConfig = [
                'key' => $key,
                'label' => (string)($definition['label'] ?? $key),
                'type' => $type,
                'value' => $value,
            ];

            if ($type === 'select' && isset($definition['options']) && is_array($definition['options'])) {
                $filterConfig['options'] = $definition['options'];
            }

            $viewFilters[] = $filterConfig;
        }

        $sortColumn = trim((string)\Shared\Helpers\Helpers::get('sort'));
        if (!in_array($sortColumn, $sortableColumns, true)) {
            $sortColumn = $defaultSortColumn;
        }

        $sortDir = strtoupper(trim((string)\Shared\Helpers\Helpers::get('dir')));
        if (!in_array($sortDir, ['ASC', 'DESC'], true)) {
            $sortDir = 'DESC';
        }

        return [
            'page' => $page,
            'perPage' => $perPage,
            'perPageOptions' => $perPageOptions,
            'filters' => $filters,
            'viewFilters' => $viewFilters,
            'queryFilters' => $queryFilters,
            'sortColumn' => $sortColumn,
            'sortDir' => $sortDir,
        ];
    }
}
?>
