<?php

namespace apexl\Io\modules\display\components;

use apexl\Io\includes\Entity;
use apexl\Io\modules\component\Collections\ComponentCollection;
use apexl\Io\modules\component\Collections\ComponentPropertyCollection;
use apexl\Io\modules\component\Factories\ComponentPropertyFactory;
use apexl\Io\modules\component\Factories\GenericComponentFactory;
use Arrayy\Type\StringCollection;

/**
 * @deprecated Builder-pattern component incompatible with the readonly Component contract — mutating methods (setFilters()/addFilterForm(), inherited setEntity() chain) fatal at runtime on write-once properties. Superseded by tabulator-based components: io-tabulator (Tabulator / EntityTabulator / AggregateTabulator); user/role admin screens via apexl/io-user-tabulator. Do not use in new code; removal planned once the legacy io-user userDisplay routes are gone.
 */
readonly class EntityFilteredDisplayTable extends EntityDisplayTable
{
    protected FilterForm $filterForm;

    public function __construct(
        GenericComponentFactory $componentFactory,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        ?string $componentId = null,
        ?Entity $entity = null,
        $replacements = [],
        $withButtons = true
    ) {
        parent::__construct(
            $componentFactory,
            $propertyFactory,
            $props,
            $components,
            $classes,
            $componentId,
            $entity,
            $replacements
        );
        if ($entity !== null) {
            $this->setFilters('orderBy[]=' . $entity->primaryKey . '&orderBy[]=DESC');

            if ($withButtons) {
                $this->addCommonButtons();
            }
        }
    }

    public function addFilterForm($routeName, $title = null, $header = 5)
    {
        $this->filterForm = (new FilterForm())
            ->src($this->routeManager->getRoutePattern($routeName))
            ->title($title)
            ->header($header);

        $this->addSupportComponent($this->filterForm);
    }
}
