<?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\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 (addTable()/addCustomTable()) 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 EntityDisplayTile extends Component
{
    public const string NAME = 'rowWrapper';

    // Untyped properties / property defaults are illegal on readonly classes
    // (pre-existing parse error). $entity and $componentWrapper are written
    // once in the constructor (clean under readonly). $footer is never used
    // anywhere in this class (dead), so `mixed` is the only inferable type.
    // $table is deliberately NOT constructor-initialised: its first write in
    // addTable()/addCustomTable() is then a legal lazy first write — but a
    // second call to either would fatal under readonly write-once semantics
    // (flagged for design review).
    protected Entity $entity;

    protected RowWrapper $componentWrapper;
    protected mixed $footer;
    protected EntityDisplayTable $table;

    protected bool $enableView;
    protected bool $enableEdit;
    protected bool $enableDelete;


    public function __construct(
        private readonly GenericComponentFactory $componentFactory,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        Entity $entity,
        ?string $componentId = null
    ) {
        parent::__construct($propertyFactory, $props, $components, $classes, $componentId);
        $this->enableView = true;
        $this->enableEdit = true;
        $this->enableDelete = true;
        $this->addClass('data-tile col-lg-12 col-xl-6');

        $this->componentWrapper = $this->componentFactory->make(RowWrapper::class)
            ->resetClasses()
            ->addClass('content-wrapper');
        $this->addClass($entity->getEntityTrueName() . '__tile-display');
        $this->entity = $entity;
        $this->addComponent($this->componentWrapper);
    }

    public function addContentTitle($title, $content = '', $header = 3)
    {
        $contentTitle = $this->componentFactory->make(ContentTitle::class)
            ->addTitle($title)
            ->addContent($content)
            ->addHeader($header);
        $this->componentWrapper->addComponent($contentTitle);
        return $this;
    }

    public function addCustomTable(EntityDisplayTable $table)
    {
        $this->table = $table;
        $this->componentWrapper->addComponent($this->table);
        return $this;
    }

    public function addTable($filters = null, $pagination = false, $dataSrc = null, $viewButton = true, $editButton = true, $deleteButton = true)
    {
        $this->table = $this->componentFactory
            ->make(EntityDisplayTable::class, constructorParams: ['entity' => $this->entity])
            ->addClass('w-100');
        $this->table->displayPagination($pagination);
        $this->table->displayShortTotals();
        if ($dataSrc) {
            $this->table->src($this->routeManager->getRoutePattern($dataSrc));
        }
        if ($filters) {
            $this->table->setFilters($filters);
        }

        if ($viewButton) {
            $this->table->addViewButton($this->entity->getEntityName() . '.display.get', !(bool) $this->enableView);
        }
        if ($editButton) {
            $this->table->addEditButton($this->entity->getEntityName() . '.display.put', !(bool) $this->enableEdit);
        }
        if ($deleteButton) {
            $this->table->addDeleteButton(
                $this->entity->getEntityName() . '.display.delete',
                !(bool) $this->enableDelete
            );
        }

        $this->componentWrapper->addComponent($this->table);
        return $this;
    }

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

    public function addButtonFooter(array $routes)
    {
        $footer = $this->componentFactory->make(RowWrapper::class)->addClass('tile-footer');
        foreach ($routes as $route => $title) {
            $footer->addComponent(
                $this->componentFactory->make(SimpleButton::class)
                    ->addRoute($this->routeManager->getRoutePattern($route))
                    ->addText($title)
            );
        }
        $this->componentWrapper->addComponent($footer);
        return $this;
    }
}
