<?php

namespace KupShop\ComponentsBundle\Twig\Components;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\GTM\PostMount\GTMViewData;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ContentBundle\View\OrdersView;
use KupShop\ContentBundle\View\OrderView;
use KupShop\GTMBundle\PageType\AccountPageType;
use KupShop\GTMBundle\PageType\OrderPageType;
use KupShop\GTMBundle\PageType\OrdersViewType;
use KupShop\GTMBundle\PageType\UserPageType;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\PreordersBundle\GTM\PageViews\Preorders;
use KupShop\PreordersBundle\View\UserPreordersView;
use KupShop\UserBundle\View\UserView;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Account/Account.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Account extends BaseComponent
{
    use GTMViewData;
    #[Required]
    public $gtmClass;

    protected ?\User $user;

    public function __construct(
        protected readonly AccountPageType $accountPageType,
        protected readonly OrdersViewType $ordersViewType,
        protected readonly OrderPageType $orderPageType,
        protected readonly UserPageType $userPageType,
        protected readonly ?Preorders $preordersPageType,
        RequestStack $requestStack,
    ) {
        $view = $requestStack->getMainRequest()?->get('view');

        $this->gtmClass = $this->accountPageType;
        if ($view instanceof OrdersView) {
            $this->gtmClass = $this->ordersViewType;
        } elseif ($view instanceof OrderView) {
            $this->gtmClass = $this->orderPageType;
        } elseif ($view instanceof UserView) {
            $this->gtmClass = $this->userPageType;
        } elseif ($view instanceof UserPreordersView) {
            $this->gtmClass = $this->preordersPageType;
        }

        $this->gtmClass->setView($view);
    }

    public function isActiveUser(): bool
    {
        return $this->getUser() !== null;
    }

    public function getUser(): ?\User
    {
        $this->user = Contexts::get(UserContext::class)->getActive();

        return $this->user;
    }
}
