<?php

namespace apexl\Io\modules\userDisplay\components;

use apexl\Io\exceptions\RouteNotFoundException;
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\user\services\CurrentUserFactory;
use Arrayy\Type\StringCollection;

final readonly class MasqueradingWarning extends Component
{
    private const string PROP__MASQUERADE_NAME = 'masqueradeName';
    private const string PROP__LEAVE_ROUTE = 'leaveRoute';

    public function __construct(
        private RouteManager $routeManager,
        private CurrentUserFactory $currentUserFactory,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        ?string $componentId = null
    ) {
        parent::__construct(
            $propertyFactory,
            $props,
            $components,
            $classes,
            $componentId
        );
    }

    /**
     * @throws RouteNotFoundException
     */
    public function init(): void
    {
        $masqueradedUser = $this->currentUserFactory->get();
        $this->setMasqueradedName(
            sprintf(
                '%s (%s)',
                $masqueradedUser->getNiceName(),
                $masqueradedUser->email
            )
        );
        $this->setLeaveRoute($this->routeManager->getRoutePattern('user.masquerade.end'));
    }

    private function setMasqueradedName(string $name): void
    {
        $this->setProperty(self::PROP__MASQUERADE_NAME, $name);
    }

    private function setLeaveRoute(string $route): void
    {
        $this->setProperty(self::PROP__LEAVE_ROUTE, $route);
    }

}
