<?php

declare(strict_types=1);

namespace Drupal\flowdrop_chat\Plugin\FlowDropNodeProcessor;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\flowdrop\Attribute\FlowDropNodeProcessor;
use Drupal\flowdrop\DTO\ParameterBagInterface;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\AbstractFlowDropNodeProcessor;
use Drupal\flowdrop_chat\Service\SystemPromptBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Executor for Get Node Types nodes.
 *
 * Fetches all available FlowDrop node type definitions and returns them as a
 * slimmed JSON string ready for injection into LLM system prompts.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "get_node_types",
  label: new TranslatableMarkup("Get Node Types"),
  description: "Fetches available FlowDrop node type definitions as a slimmed JSON string for use in LLM prompts.",
  version: "1.0.0"
)]
class GetNodeTypes extends AbstractFlowDropNodeProcessor {

  /**
   * Constructs a new GetNodeTypes processor.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\flowdrop_chat\Service\SystemPromptBuilderInterface $systemPromptBuilder
   *   The system prompt builder, used to fetch and slim node type definitions.
   */
  public function __construct(
    array $configuration,
    string $plugin_id,
    mixed $plugin_definition,
    private readonly SystemPromptBuilderInterface $systemPromptBuilder,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ): static {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('flowdrop_chat.system_prompt_builder'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function process(ParameterBagInterface $params): array {
    $exclude = $params->get('exclude', []);
    if (!is_array($exclude)) {
      $exclude = [];
    }

    return [
      'node_types' => $this->systemPromptBuilder->getSlimNodeTypesJson($exclude),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'exclude' => [
          'type' => 'array',
          'title' => 'Exclude',
          'description' => 'Node type IDs to exclude from the output.',
          'default' => [],
          'items' => [
            'type' => 'string',
          ],
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'node_types' => [
          'type' => 'string',
          'description' => 'JSON array of slimmed node type definitions (id, name, type, category, inputs, outputs, configSchema).',
        ],
      ],
    ];
  }

}
