<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product;

use KupShop\CatalogBundle\Entity\Wrapper\ProducerWrapper;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\ComponentsBundle\Twig\ProducerTooltipTrait;
use KupShop\I18nBundle\Translations\ProducersTranslation;
use KupShop\KupShopBundle\Util\System\UrlFinder;
use Query\Operator;
use Query\Translation;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

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

    protected ?ProducerWrapper $producer = null;
    protected ?Thumbnail $thumbnail = null;

    public bool $show_text = true;
    public ?string $text = null;
    public bool $show_name = true;
    public bool $show_thumbnail = true;
    public bool $show_tooltip = false;

    public function __construct(protected readonly UrlFinder $urlFinder)
    {
    }

    public function hasProducer(): bool
    {
        return $this->getProducer() !== null;
    }

    protected function getProducer(): ?ProducerWrapper
    {
        if ($this->product->idProducer === null) {
            return null;
        }

        if (!isset($this->producer)) {
            $producer = sqlQueryBuilder()->select('pr.*')
                ->from('producers', 'pr')
                ->andWhere(Operator::equals(['pr.id' => $this->product->idProducer]))
                ->andWhere('pr.active="Y"')
                ->andWhere(Translation::coalesceTranslatedFields(ProducersTranslation::class,
                    ['name' => 'name', 'data' => 'data_translated']))
                ->execute()->fetchAssociative();
            if (empty($producer)) {
                return null;
            }

            $producer['data'] = array_replace_recursive(json_decode($producer['data'], true) ?: [], json_decode($producer['data_translated'], true) ?: []);

            $this->producer = (new ProducerWrapper())->setObject($producer);
        }

        return $this->producer;
    }

    public function getId(): ?int
    {
        $producer = $this->getProducer();

        return $producer['id'] ?? null;
    }

    public function getName(): string
    {
        $producer = $this->getProducer();

        return $producer['name'] ?? '';
    }

    public function getUrl(): ?string
    {
        $producer = $this->getProducer();

        return $producer['url'] ?? null;
    }

    public function getThumbnail(): ?Thumbnail
    {
        if ($this->thumbnail === null) {
            $producer = $this->getProducer();

            if ($producer['photo'] ?? null) {
                $this->thumbnail = new Thumbnail($this->getId(), $this->getName(), $this->producer['date_updated']);
            }
        }

        return $this->thumbnail;
    }

    public function getTooltip(): string
    {
        $producer = $this->getProducer()->getObject();

        return $this->getProducerTooltip($producer);
    }
}
