<?php

declare(strict_types=1);

namespace Drupal\flowdrop_node_processor\Plugin\FlowDropNodeProcessor;

use Drupal\flowdrop\Attribute\FlowDropNodeProcessor;
use Drupal\flowdrop\DTO\ParameterBagInterface;
use Drupal\flowdrop\DTO\ValidationResult;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\AbstractFlowDropNodeProcessor;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Executor for Chat Input nodes.
 *
 * Provides chat input functionality for playground workflows.
 * Accepts message content from playground and optionally includes
 * conversation history for context-aware processing.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "chat_input",
  label: new \Drupal\Core\StringTranslation\TranslatableMarkup("Chat Input"),
  description: "Chat input for playground workflows with optional conversation history",
  version: "1.0.0"
)]
class ChatInput extends AbstractFlowDropNodeProcessor {

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ): static {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function validateParams(array $params): ValidationResult {
    // Chat input can accept any inputs or none.
    // Message content is typically injected via initialData.
    return ValidationResult::success();
  }

  /**
   * {@inheritdoc}
   */
  public function process(ParameterBagInterface $params): array {
    // Get message content from parameter
    // (injected via initialData[nodeId]['message']).
    $message = $params->get("message", "");

    // Get optional conversation history
    // (injected via initialData[nodeId]['history']).
    $history = $params->get("history", []);
    if (!is_array($history)) {
      $history = [];
    }

    // Get session and message IDs if available (for tracking).
    $sessionId = $params->get("sessionId");
    $messageId = $params->get("messageId");

    // Build output with message and optional history.
    $output = [
      "message" => $message,
    ];

    // Include history if provided and not empty.
    if (!empty($history)) {
      $output["history"] = $history;
    }

    // Include metadata if available.
    if ($sessionId !== NULL) {
      $output["sessionId"] = $sessionId;
    }
    if ($messageId !== NULL) {
      $output["messageId"] = $messageId;
    }

    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      "type" => "object",
      "properties" => [
        "message" => [
          "type" => "string",
          "title" => "Message",
          "description" => "The chat message content (typically injected from playground)",
          "default" => "",
        ],
        "history" => [
          "type" => "array",
          "title" => "Conversation History",
          "description" => "Optional conversation history for context-aware processing",
          "items" => [
            "type" => "object",
            "properties" => [
              "role" => [
                "type" => "string",
                "description" => "Message role (user, assistant)",
              ],
              "content" => [
                "type" => "string",
                "description" => "Message content",
              ],
              "timestamp" => [
                "type" => "integer",
                "description" => "Message timestamp",
              ],
            ],
          ],
        ],
        "sessionId" => [
          "type" => "integer",
          "title" => "Session ID",
          "description" => "Playground session ID (for tracking)",
        ],
        "messageId" => [
          "type" => "integer",
          "title" => "Message ID",
          "description" => "Playground message ID (for tracking)",
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      "type" => "object",
      "properties" => [
        "message" => [
          "type" => "string",
          "description" => "The chat message content",
        ],
        "history" => [
          "type" => "array",
          "description" => "Conversation history (if included)",
          "items" => [
            "type" => "object",
            "properties" => [
              "role" => [
                "type" => "string",
                "description" => "Message role",
              ],
              "content" => [
                "type" => "string",
                "description" => "Message content",
              ],
              "timestamp" => [
                "type" => "integer",
                "description" => "Message timestamp",
              ],
            ],
          ],
        ],
        "sessionId" => [
          "type" => "integer",
          "description" => "Playground session ID",
        ],
        "messageId" => [
          "type" => "integer",
          "description" => "Playground message ID",
        ],
      ],
    ];
  }

}
