<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiAdmin\Types\Return;

use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\GraphQLBundle\ApiShared\Types\Price\Price;
use KupShop\GraphQLBundle\ApiShared\Types\Product\Product;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
class ReturnReplacement
{
    public function __construct(
        public array $replacement,
        public readonly ProductCollection $productCollection,
    ) {
    }

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

    #[Field(description: 'ID produktu.')]
    public function getProductId(): int
    {
        return $this->replacement['id_product'];
    }

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

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

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

    #[Field(description: 'Celková cena.')]
    public function getTotalPrice(): Price
    {
        return new Price(
            PriceCalculator::mul($this->replacement['priceObject'], toDecimal($this->replacement['pieces'])),
            false
        );
    }

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

        return null;
    }
}
