<?php

namespace KupShop\LLMBundle\TextObjects;

use KupShop\LLMBundle\Dto\TextPromptInput;
use KupShop\LLMBundle\Enum\PromptPlaceholder;

abstract class AbstractProductObject extends AbstractTextObject
{
    public function providesPlaceholders(): array
    {
        $placeholders = array_merge(parent::providesPlaceholders(), [
            PromptPlaceholder::PRODUCT_PARAMETERS,
            PromptPlaceholder::PRODUCT_NAME,
            PromptPlaceholder::PRODUCT_PHOTO,
            PromptPlaceholder::PRODUCT_DESCRIPTION,
            PromptPlaceholder::PRODUCT_ANNOTATION,
            PromptPlaceholder::PRODUCER_NAME,
        ]);

        if (findModule(\Modules::PRODUCTS, \Modules::SUB_DESCR_PLUS)) {
            $placeholders[] = PromptPlaceholder::PRODUCT_DESCRIPTION_PLUS;
        }

        if (findModule(\Modules::TEMPLATES)) {
            $placeholders[] = PromptPlaceholder::PRODUCT_TEMPLATES;
        }

        return $placeholders;
    }

    final protected function setPromptPlaceholders(TextPromptInput $prompt, ?string $entityType, ?int $entityId): void
    {
        $providesPlaceholders = $this->providesPlaceholders();

        if ($entityType == 'products' && $entityId) {
            $product = new \Product($entityId);
            $product->createFromDB();
            $product->fetchParameters();
            $product->fetchProducer();
            $product->fetchImages('default_feed');

            in_array(PromptPlaceholder::PRODUCT_NAME, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_NAME, $product->title);

            in_array(PromptPlaceholder::PRODUCT_PARAMETERS, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_PARAMETERS, $this->placeholdersCreator->getProductParameters($product));

            in_array(PromptPlaceholder::PRODUCT_ANNOTATION, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_ANNOTATION, $product->descr);

            in_array(PromptPlaceholder::PRODUCT_DESCRIPTION, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_DESCRIPTION, $product->longDescr);

            in_array(PromptPlaceholder::PRODUCT_TEMPLATES, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_TEMPLATES, $this->placeholdersCreator->getProductsTemplates($product->id));

            in_array(PromptPlaceholder::PRODUCT_DESCRIPTION_PLUS, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_DESCRIPTION_PLUS, $this->placeholdersCreator->getTextFromBlockId($product->id_block));

            in_array(PromptPlaceholder::PRODUCT_PHOTO, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCT_PHOTO, $this->placeholdersCreator->getImageFromArray($product->image));

            in_array(PromptPlaceholder::PRODUCER_NAME, $providesPlaceholders)
            && $prompt->replacePlaceholder(PromptPlaceholder::PRODUCER_NAME, $product->producer['title'] ?? '');
        }
    }
}
