{% import '@lib/di.twig' as di %}
<?php

declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\AiAutomatorType;

{% apply sort_namespaces %}
{% if base_class == 'RuleBase' or base_class == 'ExternalBase' %}
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
{% endif %}
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ai_automators\Attribute\AiAutomatorType;
use Drupal\ai_automators\PluginBaseClasses\{{ base_class }};
use Drupal\ai_automators\PluginInterfaces\AiAutomatorTypeInterface;
{% endapply %}

/**
 * The rules for a {{ field_rule }} field.
 */
#[AiAutomatorType(
  id: '{{ plugin_id }}',
  label: new TranslatableMarkup('{{ plugin_label }}'),
  field_rule: '{{ field_rule }}',
  target: '{{ target }}',
)]
class {{ class }} extends {{ base_class }} implements AiAutomatorTypeInterface {

  /**
   * {@inheritDoc}
   */
  public $title = '{{ plugin_label }}';

{% if base_class != 'ExternalBase' and not needs_prompt %}
  /**
   * {@inheritDoc}
   */
  public function needsPrompt() {
    return FALSE;
  }

  /**
   * {@inheritDoc}
   */
  public function advancedMode() {
    return FALSE;
  }

{% endif %}
{% if base_class == 'RuleBase' and needs_prompt %}
  /**
   * {@inheritDoc}
   */
  public function helpText() {
    return "This automator generates values for {{ field_rule }} fields.";
  }

  /**
   * {@inheritDoc}
   */
  public function generate(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $prompts = parent::generate($entity, $fieldDefinition, $automatorConfig);

    // Append JSON format instruction.
    foreach ($prompts as $key => $prompt) {
      $prompt .= "\n\nDo not include any explanations, only provide a RFC8259 "
        . "compliant JSON response following this format without deviation.\n"
        . '[{"value": "the generated value"}]' . "\n";
      $prompts[$key] = $prompt;
    }

    $total = [];
    $instance = $this->prepareLlmInstance('chat', $automatorConfig);
    foreach ($prompts as $prompt) {
      $values = $this->runChatMessage($prompt, $automatorConfig, $instance, $entity);
      if (!empty($values)) {
        $total = array_merge_recursive($total, $values);
      }
    }
    return $total;
  }

  /**
   * {@inheritDoc}
   */
  public function verifyValue(ContentEntityInterface $entity, $value, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    // @todo Add custom validation logic.
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function storeValues(ContentEntityInterface $entity, array $values, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $entity->set($fieldDefinition->getName(), $values);
  }

{% endif %}
{% if base_class == 'RuleBase' and not needs_prompt %}
  /**
   * {@inheritDoc}
   */
  public function allowedInputs() {
    return ['text_long', 'text', 'string', 'string_long', 'text_with_summary'];
  }

  /**
   * {@inheritDoc}
   */
  public function generate(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $values = [];
    foreach ($entity->get($automatorConfig['base_field']) as $item) {
      if ($item->value) {
        // @todo Implement your custom transformation logic here.
        $values[] = $item->value;
      }
    }
    return $values;
  }

  /**
   * {@inheritDoc}
   */
  public function verifyValue(ContentEntityInterface $entity, $value, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function storeValues(ContentEntityInterface $entity, array $values, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $entity->set($fieldDefinition->getName(), $values);
  }

{% endif %}
{% if base_class == 'ExternalBase' %}
  /**
   * {@inheritDoc}
   */
  public function needsPrompt() {
    return FALSE;
  }

  /**
   * {@inheritDoc}
   */
  public function advancedMode() {
    return FALSE;
  }

  /**
   * {@inheritDoc}
   */
  public function allowedInputs() {
    return ['text_long', 'text', 'string', 'string_long', 'text_with_summary'];
  }

  /**
   * {@inheritDoc}
   */
  public function generate(ContentEntityInterface $entity, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $values = [];
    foreach ($entity->get($automatorConfig['base_field']) as $item) {
      if ($item->value) {
        // @todo Implement your custom transformation logic here.
        $values[] = $item->value;
      }
    }
    return $values;
  }

  /**
   * {@inheritDoc}
   */
  public function verifyValue(ContentEntityInterface $entity, $value, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function storeValues(ContentEntityInterface $entity, array $values, FieldDefinitionInterface $fieldDefinition, array $automatorConfig) {
    $entity->set($fieldDefinition->getName(), $values);
  }

{% endif %}
}
