<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiPublic\Controller;

use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
use KupShop\GraphQLBundle\ApiShared\Types\Seller\Seller;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\SellerBundle\Context\SellerContext;
use KupShop\SellerBundle\Utils\SellerUtil;
use TheCodingMachine\GraphQLite\Annotations\Query;

class SellerController
{
    public function __construct(private ?SellerUtil $sellerUtil)
    {
    }

    /**
     * Načte aktivní prodejnu.
     */
    #[Query]
    #[Module(\Modules::JS_SHOP)]
    public function seller(): ?Seller
    {
        if (!findModule(\Modules::SELLERS)) {
            return null;
        }

        $sellerContext = Contexts::get(SellerContext::class);

        $sellerId = $sellerContext->getActiveId();

        if ($sellerId && ($seller = $this->sellerUtil->getSeller((int) $sellerId))) {
            $sellerContext->remember($sellerId);

            return new Seller($seller);
        }

        return null;
    }
}
