<?php

use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\LLMBundle\Util\TextObjectUtil;

class llmPromptList extends BaseList
{
    protected $tableName = 'llm_prompts';
    protected ?string $tableAlias = 'lp';

    protected $tableDef = [
        'id' => 'lp.id',
        'fields' => [
            'title' => ['field' => 'lp.title', 'translate' => true, 'size' => 2],
            'user_text' => ['field' => 'user_text', 'translate' => true, 'size' => 4],
            'text_objects' => ['render' => 'renderTextObjects', 'translate' => true, 'size' => 2],
            'type' => ['field' => 'label', 'render' => 'renderType', 'translate' => true, 'size' => 1],
            'figure' => ['field' => 'figure', 'render' => 'renderBoolean', 'translate' => true, 'size' => 0.5],
        ],
    ];

    public function renderTextObjects($values)
    {
        $textObjectsUtil = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(TextObjectUtil::class);
        $names = array_map(function ($label) use ($textObjectsUtil) {
            return $textObjectsUtil->getByLabel($label)->getDescription();
        }, $values['text_objects'] ? explode(',', $values['text_objects']) : []);

        return implode(', ', $names);
    }

    public function renderType($values)
    {
        if ($values['label']) {
            return 'Výchozí';
        }

        return 'Vlastní';
    }

    public function getQuery()
    {
        return parent::getQuery()
            ->addSelect('GROUP_CONCAT(lpo.object_type) as text_objects')
            ->leftJoin('lp', 'llm_prompts_objects', 'lpo', 'lpo.id_prompt = lp.id')
            ->groupBy('lp.id');
    }
}
