<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiAdmin\Types\Purchase;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\OrderDiscountBundle\Util\DiscountManager;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class DiscountCode
{
    public function __construct(
        #[Field(description: 'Slevový kód / dárkový poukaz')] public string $code,
    ) {
    }

    #[Field(description: 'ID slevy, ke které se kód stahuje.')]
    public function getDiscountId(): ?int
    {
        if ($this->getDiscountManager()->couponNumberExists($this->code, $orderDiscounts)) {
            return reset($orderDiscounts)['id'] ?? null;
        }

        return null;
    }

    private function getDiscountManager(): DiscountManager
    {
        static $discountManager;

        if (null === $discountManager) {
            $discountManager = ServiceContainer::getService(DiscountManager::class);
        }

        return $discountManager;
    }
}
