<?php

namespace KupShop\GraphQLBundle\ApiPos\Controller;

use KupShop\GraphQLBundle\ApiPos\Types\Collection\PosProductsInfoCollection;
use KupShop\KupShopBundle\Context\PosContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\POSBundle\Util\PosProductInfoUtil;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Service\Attribute\Required;
use TheCodingMachine\GraphQLite\Annotations\Query;

class PosProductInfoController
{
    private ?PosProductInfoUtil $productInfoUtil = null;

    #[Required]
    public LoggerInterface $logger;

    #[Query]
    public function posProductInfo(?string $idProduct = null, ?string $ean = null, ?string $code = null): PosProductsInfoCollection
    {
        $posEntity = Contexts::get(PosContext::class)->getActive();
        try {
            return $this->productInfoUtil->getProductInfoData($posEntity, $idProduct, $ean, $code);
        } catch (\Exception $e) {
            $this->logger->notice('POS API: posProductInfo', ['data' => [
                'requestParams' => serialize([
                    'idPos' => $posEntity->getId(),
                    'idProduct' => $idProduct,
                    'ean' => $ean,
                    'code' => $code,
                ]),
                'response' => $e->getMessage(),
                'type' => 'exception',
            ]]);

            return new PosProductsInfoCollection([]);
        }
    }

    #[Required]
    public function setProductInfoUtil(?PosProductInfoUtil $productInfoUtil): void
    {
        $this->productInfoUtil = $productInfoUtil;
    }
}
