<?php

namespace KupShop\GraphQLBundle\ApiPublic\Types\Cart;

use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\GraphQLBundle\ApiPublic\Types\Product\ProductPublic;
use KupShop\GraphQLBundle\ApiShared\Types\Price\Price;
use KupShop\OrderingBundle\Entity\Purchase\ProductPurchaseItem;
use KupShop\OrderingBundle\Util\Purchase\PurchaseItemUtil;
use TheCodingMachine\GraphQLite\Annotations\Autowire;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class CartItem
{
    private $item;
    private ProductCollection $productCollection;

    public function __construct(ProductPurchaseItem $item, ProductCollection $productCollection)
    {
        $this->item = $item;
        $this->productCollection = $productCollection;
    }

    #[Field]
    public function getId(): int
    {
        return (int) $this->item->getId();
    }

    #[Field]
    public function getProductId(): int
    {
        return (int) $this->item->getIdProduct();
    }

    #[Field]
    public function getVariationId(): ?int
    {
        return $this->item->getIdVariation() ? (int) $this->item->getIdVariation() : null;
    }

    #[Field]
    public function getName(): string
    {
        return $this->item->getName();
    }

    #[Field]
    public function getStep(): float
    {
        return $this->item->getStep();
    }

    #[Field]
    public function getPieces(): float
    {
        return $this->item->getPieces();
    }

    #[Field]
    public function getPiecePrice(): Price
    {
        return new Price(
            $this->item->getPiecePrice(), round: false
        );
    }

    #[Field]
    public function getTotalPrice(): Price
    {
        return new Price(
            $this->item->getPrice(), round: false
        );
    }

    #[Field]
    public function getPiecePriceWithDiscounts(): Price
    {
        return new Price(
            $this->item->getPiecePriceWithDiscounts(), round: false
        );
    }

    #[Field]
    public function getTotalPriceWithDiscounts(): Price
    {
        return new Price(
            $this->item->getPriceWithDiscounts(), round: false
        );
    }

    /**
     * @Field()
     *
     * @Autowire(for="$purchaseItemUtil")
     */
    public function getPriceOriginal(PurchaseItemUtil $purchaseItemUtil): Price
    {
        $priceOriginal = $purchaseItemUtil->getProductItemOriginalPrice($this->item);

        return new Price(
            $priceOriginal, round: false
        );
    }

    /**
     * Sleva na položce.
     *
     * @Field
     *
     * @Autowire(for="$purchaseItemUtil")
     */
    public function getDiscount(PurchaseItemUtil $purchaseItemUtil): float
    {
        return $purchaseItemUtil->getProductItemDiscount($this->item)->asFloat();
    }

    #[Field]
    public function getProduct(): ProductPublic
    {
        return new ProductPublic(
            $this->item->getProduct(),
            $this->productCollection
        );
    }

    /**
     * @Field()
     */
    public function getNote(): ?string
    {
        $note = $this->item->getNote();

        return is_string($note) ? $note : json_encode($note ?: new \stdClass());
    }

    #[Field]
    public function getPrintNote(): string
    {
        $note = $this->item->getNote();

        return $this->item->getProduct()->printNote($note);
    }

    #[Field]
    public function getGtmActions(): GtmActions
    {
        $this->productCollection->fetchVariationsEntities(false);

        return new GtmActions($this->item);
    }
}
