<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiAdmin\Types\Sale;

use KupShop\GraphQLBundle\ApiShared\Types\Price\Price;
use KupShop\GraphQLBundle\ApiShared\Types\Product\Product;
use KupShop\SalesBundle\SaleList\SaleCollection;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class SaleItem
{
    public function __construct(
        private readonly \KupShop\SalesBundle\Entity\SaleItem $item,
        public SaleCollection $saleCollection,
    ) {
    }

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

    #[Field(description: 'Interní ID produktu.')]
    public function getProductId(): ?int
    {
        return $this->item->productId;
    }

    #[Field(description: 'Interní ID varianty.')]
    public function getVariationId(): ?int
    {
        return $this->item->variationId;
    }

    #[Field(description: 'Počet objednaných kusů.')]
    public function getPieces(): float
    {
        return $this->item->pieces;
    }

    #[Field(description: 'Cena za kus.')]
    public function getPiecePrice(): Price
    {
        return new Price(
            $this->item->piecePrice,
            false
        );
    }

    #[Field(description: 'Celková cena.')]
    public function getTotalPrice(): Price
    {
        return new Price(
            $this->item->totalPrice,
            false
        );
    }

    #[Field(description: 'Název položky.')]
    public function getName(): string
    {
        return $this->item->name;
    }

    #[Field(description: 'Produkt.')]
    public function getProduct(): ?Product
    {
        if ($product = $this->item->getProduct()) {
            return new Product(
                $product,
                $this->saleCollection->getProductCollection(),
            );
        }

        return null;
    }
}
