<?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 Build System Prompt nodes.
 *
 * Combines a slimmed node types JSON string and the current workflow state
 * into a fully-interpolated FlowDrop chat system prompt. Intended for use
 * in workflows that replace the builtin PHP chat processing pipeline.
 *
 * Wire GetNodeTypes.node_types → node_types, plus TextInput for state.
 * Wire output system_prompt to a Chat node's systemPrompt port.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "build_system_prompt",
  label: new TranslatableMarkup("Build System Prompt"),
  description: "Combines node types and workflow state into a FlowDrop chat system prompt.",
  version: "1.0.0"
)]
class BuildSystemPrompt extends AbstractFlowDropNodeProcessor {

  /**
   * Constructs a new BuildSystemPrompt 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 service.
   */
  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 {
    $workflowState = $params->get('workflow_state');
    $nodeTypesJson = $params->getString('node_types', '[]');
    $template = $params->getString('template', '');

    $systemPrompt = $this->systemPromptBuilder->buildWithNodeTypesJson(
      $workflowState,
      $nodeTypesJson,
      $template,
    );

    return [
      'system_prompt' => $systemPrompt,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'workflow_state' => [
          'type' => 'mixed',
          'title' => 'Workflow State',
          'description' => 'The current workflow state (array or JSON string). Wire from the workflow_state input.',
        ],
        'node_types' => [
          'type' => 'string',
          'title' => 'Node Types JSON',
          'description' => 'Slimmed node types JSON. Wire from a GetNodeTypes node output.',
          'default' => '[]',
        ],
        'template' => [
          'type' => 'string',
          'title' => 'Template Override',
          'description' => 'Optional system prompt template override. Leave empty to use the configured default.',
          'default' => '',
          'format' => 'multiline',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'system_prompt' => [
          'type' => 'string',
          'description' => 'The fully-interpolated system prompt, ready to wire to a Chat node systemPrompt input.',
        ],
      ],
    ];
  }

}
