<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product;

use KupShop\CatalogBundle\Entity\Product\ProductPricesResult;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Interfaces\Microdata;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\MicrodataProvider;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\KupShopBundle\Util\Price\ProductPrice;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;
use KupShop\UnitsBundle\Utils\MeasureUnitUtil;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Product/Price/Price.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Price extends BaseComponent implements Microdata
{
    use ProductDataTrait;

    public function __construct(private readonly PurchaseUtil $purchaseUtil, private readonly ?MeasureUnitUtil $unitPriceUtil = null)
    {
    }

    protected ProductPricesResult $productPricesResult;

    public array $note = [];
    public bool $show_price_without_vat = false;
    public bool $show_original_price = true;
    public bool $show_price_novat_text = false;
    public bool $show_original_price_info_text = false;
    public bool $show_retail_price = false;
    public bool $show_retail_price_discount = true;
    public bool $show_discount_flag = false;
    public bool $show_from_price = true;
    public bool $show_moc_text_tooltip = false;
    public bool $show_moc_discount_tooltip = false;
    public bool $show_unit_price = false;
    public bool $show_price_withoutvat_text = false;
    public bool $show_price_variations_withoutvat_text = true;

    protected function getProductPricesResult(): ProductPricesResult
    {
        if (!isset($this->productPricesResult)) {
            $this->productPricesResult = $this->provider->getProductPricesResult($this->id_product, $this->id_variation);
        }

        return $this->productPricesResult;
    }

    public function checkMaxPrice(): bool
    {
        if ($this->show_from_price && $this->hasVariations() && !$this->id_variation) {
            return PriceCalculator::firstLower($this->getPrice(), $this->product->priceMax);
        }

        return false;
    }

    public function getPrice(): ProductPrice
    {
        if (!empty($this->note)) {
            return $this->purchaseUtil->getItemPrice([
                'pieces' => 1,
                'note' => json_encode($this->note),
                'id_variation' => null,
                'vat' => $this->product->vat_id,
            ], $this->product);
        }

        return $this->getProductPricesResult()->getPrice();
    }

    public function getOriginalPrice(): ?\KupShop\KupShopBundle\Util\Price\Price
    {
        return $this->getProductPricesResult()->getOriginalPrice();
    }

    public function getRetailPrice(): ?\KupShop\KupShopBundle\Util\Price\Price
    {
        return $this->getProductPricesResult()->getRetailPrice();
    }

    public function getDiscount(): float
    {
        return $this->getProductPricesResult()->getDiscount()->asFloat();
    }

    public function getRetailPriceDiscount(): float
    {
        return $this->getProductPricesResult()->getRetailPriceDiscount()->asFloat();
    }

    public function getUnitPrice(bool $round = false): ?array
    {
        if ($this->unitPriceUtil) {
            return $this->unitPriceUtil->getUnitPrice($this->getProductUnified(), $round);
        }

        return [];
    }

    public function initMicrodata(): array
    {
        $microdata = [
            'priceCurrency' => Contexts::get(CurrencyContext::class)->getActiveId(),
            'price' => $this->getPrice()->getPriceWithVat()->printFloatValue(-2),
            'priceValidUntil' => (new \DateTime())->add(new \DateInterval('P31D'))->format('Y-m-d'),
        ];

        return [
            MicrodataProvider::PRODUCT => [
                'offers' => $microdata,
            ],
        ];
    }
}
