<?php

namespace apexl\Io\modules\display\components;

use apexl\Io\includes\RouteManager;
use apexl\Io\modules\component\Collections\ComponentCollection;
use apexl\Io\modules\component\Collections\ComponentPropertyCollection;
use apexl\Io\modules\component\Component\Component;
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 (addData(), hideDisabled()) 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. No known usages anywhere in the estate (orphaned).
 */
readonly class BasicTable extends Component
{
    protected array $data;
    // Property default values are illegal on readonly classes (pre-existing
    // parse error) — initialised in the constructor instead. NOTE: the
    // hideDisabled() setter reassigns this post-construction, which is
    // incompatible with readonly write-once semantics — flagged for design
    // review; only the parse error is fixed here.
    protected bool $hideDisabled;

    public function __construct(
        $data,
        private readonly RouteManager $routeManager,
        private readonly GenericComponentFactory $componentFactory,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        ?string $componentId = null
    ) {
        parent::__construct($propertyFactory, $props, $components, $classes, $componentId);
        $this->hideDisabled = false;
        $this->addData($data);
    }

    public function addData($data)
    {
        $this->setProperty('data', $data);
        return $this;
    }

    public function colspans($colspans = [])
    {
        $this->setProperty('colspans', $colspans);
        return $this;
    }

    public function hideIdColumn($display = true)
    {
        $this->setProperty('hideIdColumn', $display);
        return $this;
    }

    public function addViewButton($routeName, $disabled = false)
    {
        $this->addButton($routeName, 'eye', $disabled);
        return $this;
    }

    public function addButton($routeName, $icon = '', $disabled = false)
    {
        $BEM = str_replace('Entity', '', (string) $this->entity->getEntityName());
        $button = $this->componentFactory->make(TableButton::class)
            ->addRoute($this->routeManager->getRoutePattern($routeName, [], false))
            ->addIcon($icon)
            ->addClass($BEM . '_table__btn')
            ->addClass($BEM . '_table__btn-' . $icon);
        if ($disabled) {
            $button->addRoute('#');
            $button->addClass($BEM . '_table__btn-disabled');
            if ($this->hideDisabled) {
                //do not add the button to the component
                return $this;
            }
        }
        $this->addComponent($button);
        return $this;
    }

    public function addEditButton($routeName, $disabled = false)
    {
        $this->addButton($routeName, 'edit', $disabled);
        return $this;
    }

    public function addDeleteButton($routeName, $disabled = false)
    {
        $this->addButton($routeName, 'trash', $disabled);
        return $this;
    }

    public function hideDisabled($value = true)
    {
        $this->hideDisabled = $value;
        return $this;
    }
}
