<?php

declare(strict_types=1);

namespace App\Support\Reports;

/**
 * One filter control in a report's toolbar.
 *
 * `key` is the query-string parameter the generic filter bar reads and
 * writes (`search`, `date_from/date_to` for a `DateRange`, or a
 * report-chosen name like `status`/`category_id` for a `Select`). What that
 * key is filtered against lives entirely inside the report's own query
 * closure — this object only describes how to draw the control.
 */
final readonly class ReportFilter
{
    /**
     * @param  array<int|string, string>  $options  value => label, for Select only
     */
    public function __construct(
        public string $key,
        public string $label,
        public ReportFilterType $type,
        public array $options = [],
        public ?string $placeholder = null,
    ) {}

    public static function search(string $placeholder): self
    {
        return new self('search', 'Search', ReportFilterType::Search, [], $placeholder);
    }

    /**
     * @param  array<int|string, string>  $options
     */
    public static function select(string $key, string $label, array $options, ?string $placeholder = null): self
    {
        return new self($key, $label, ReportFilterType::Select, $options, $placeholder ?? 'Any '.mb_strtolower($label));
    }

    public static function dateRange(string $label): self
    {
        return new self('date', $label, ReportFilterType::DateRange);
    }
}
