<?php

declare(strict_types=1);

namespace Drupal\flowdrop_interrupt\Plugin\FlowDropNodeProcessor;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\flowdrop\Attribute\FlowDropNodeProcessor;
use Drupal\flowdrop\DTO\ExecutionContextDTO;
use Drupal\flowdrop\DTO\ParameterBagInterface;
use Drupal\flowdrop\DTO\ValidationResult;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\AbstractFlowDropNodeProcessor;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\ExecutionContextAwareInterface;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\ResumableNodeProcessorInterface;
use Drupal\flowdrop_interrupt\Message\HitlInterruptMessage;
use Drupal\flowdrop_interrupt\Enum\InterruptType;
use Drupal\flowdrop_interrupt\Exception\InterruptRequiredException;
use Drupal\flowdrop_orchestration\DTO\ResolvedInterruptInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form input node processor for human-in-loop workflows.
 *
 * Pauses workflow execution and presents a JSON Schema-based form to the user.
 * This is the most flexible input type, allowing complex data collection.
 *
 * Config:
 * - message: The prompt message to display above the form.
 * - schema: JSON Schema defining the form fields.
 * - default_values: Default values for form fields.
 *
 * Outputs:
 * - data: The form data as an object matching the schema.
 * - response_time: Timestamp when the response was received.
 * - user_id: ID of the user who responded (if authenticated).
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "form_input",
  label: new TranslatableMarkup("Form Input"),
  description: "Pauses workflow for user to fill a JSON Schema form",
  version: "1.0.0"
)]
class FormInputNode extends AbstractFlowDropNodeProcessor implements ExecutionContextAwareInterface, ResumableNodeProcessorInterface {

  /**
   * The execution context.
   *
   * @var \Drupal\flowdrop\DTO\ExecutionContextDTO|null
   */
  protected ?ExecutionContextDTO $executionContext = NULL;

  /**
   * {@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 setExecutionContext(ExecutionContextDTO $context): void {
    $this->executionContext = $context;
  }

  /**
   * {@inheritdoc}
   */
  public function validateParams(array $params): ValidationResult {
    if (empty($params["message"])) {
      return ValidationResult::error("message", "Prompt message is required.", "REQUIRED");
    }

    if (empty($params["schema"]) || !is_array($params["schema"])) {
      return ValidationResult::error("schema", "JSON Schema is required.", "REQUIRED");
    }

    // Basic JSON Schema validation.
    $schema = $params["schema"];
    if (!isset($schema["type"])) {
      return ValidationResult::error("schema", "Schema must have a 'type' property.", "INVALID_SCHEMA");
    }

    return ValidationResult::success();
  }

  /**
   * {@inheritdoc}
   *
   * First-time entry only. The orchestrator routes resume invocations to
   * {@see self::resume()} via the ResumableNodeProcessorInterface contract.
   */
  public function process(ParameterBagInterface $params): array {
    $message = $params->getString("message", "Please fill out the form");
    $schema = $params->getArray("schema", []);
    $defaultValues = $params->getArray("default_values", []);

    throw new InterruptRequiredException(new HitlInterruptMessage(
      type: InterruptType::SchemaForm,
      message: $message,
      schema: $schema,
      defaultValue: !empty($defaultValues) ? $defaultValues : NULL,
    ), [], "Workflow paused for user form input");
  }

  /**
   * {@inheritdoc}
   *
   * Returns the form data as an array.
   */
  public function resume(
    ResolvedInterruptInterface $interrupt,
    ParameterBagInterface $params,
  ): array {
    $responseData = $interrupt->getResponseData();
    $data = is_array($responseData) ? $responseData : [];

    return [
      "data" => $data,
      "response_time" => $interrupt->getResolvedAt() ?? time(),
      "user_id" => $interrupt->getResolvedBy(),
      "interrupt_id" => $interrupt->getInterruptId(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      "type" => "object",
      "properties" => [
        "message" => [
          "type" => "string",
          "title" => "Prompt Message",
          "description" => "The message to display above the form",
          "default" => "Please fill out the form",
          "format" => "multiline",
        ],
        "schema" => [
          "type" => "object",
          "title" => "Form Schema",
          "description" => "JSON Schema defining the form fields",
          "format" => "json",
          "default" => [
            "type" => "object",
            "properties" => [
              "name" => [
                "type" => "string",
                "title" => "Name",
              ],
            ],
          ],
        ],
        "default_values" => [
          "type" => "object",
          "title" => "Default Values",
          "description" => "Default values for form fields",
          "format" => "json",
        ],
        "__interrupt_id__" => [
          "type" => "string",
          "description" => "Internal: Interrupt ID for resume operations",
          "connectable" => FALSE,
          "configurable" => FALSE,
        ],
      ],
      "required" => ["message", "schema"],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      "type" => "object",
      "properties" => [
        "data" => [
          "type" => "object",
          "description" => "The form data submitted by the user",
        ],
        "response_time" => [
          "type" => "integer",
          "description" => "Unix timestamp when the user responded",
        ],
        "user_id" => [
          "type" => "integer",
          "description" => "ID of the user who responded (null if anonymous)",
        ],
        "interrupt_id" => [
          "type" => "string",
          "description" => "UUID of the interrupt that was resolved",
        ],
      ],
    ];
  }

}
