<?php

declare(strict_types=1);

namespace Drupal\flowdrop_node_processor\Plugin\FlowDropNodeProcessor;

use Drupal\Core\StringTranslation\TranslatableMarkup;
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;

/**
 * Generates a numeric sequence for use with ForEach loops.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "range",
  label: new TranslatableMarkup("Range"),
  description: "Generate a numeric sequence (e.g., 1 to 10)",
  version: "1.0.0"
)]
class Range 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 {
    if (!isset($params['end'])) {
      return ValidationResult::error('end', 'End value is required', 'MISSING_END');
    }
    $step = $params['step'] ?? 1;
    if ($step === 0) {
      return ValidationResult::error('step', 'Step cannot be zero', 'INVALID_STEP');
    }
    return ValidationResult::success();
  }

  /**
   * {@inheritdoc}
   */
  public function process(ParameterBagInterface $params): array {
    $start = $params->getInt('start', 1);
    $end = $params->getInt('end', 1);
    $step = $params->getInt('step', 1);

    // Auto-correct step direction if needed.
    if (($start > $end && $step > 0) || ($start < $end && $step < 0)) {
      $step = -$step;
    }

    $items = range($start, $end, abs($step));

    return [
      'items' => $items,
      'count' => count($items),
      'start' => $start,
      'end' => $end,
      'step' => $step,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'start' => [
          'type' => 'integer',
          'title' => 'Start',
          'description' => 'Starting value of the sequence',
          'default' => 1,
        ],
        'end' => [
          'type' => 'integer',
          'title' => 'End',
          'description' => 'Ending value of the sequence (inclusive)',
        ],
        'step' => [
          'type' => 'integer',
          'title' => 'Step',
          'description' => 'Increment between values',
          'default' => 1,
        ],
      ],
      'required' => ['end'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'items' => [
          'type' => 'array',
          'description' => 'The generated numeric sequence',
        ],
        'count' => [
          'type' => 'integer',
          'description' => 'Number of items in the sequence',
        ],
        'start' => [
          'type' => 'integer',
          'description' => 'Start value used',
        ],
        'end' => [
          'type' => 'integer',
          'description' => 'End value used',
        ],
        'step' => [
          'type' => 'integer',
          'description' => 'Step value used',
        ],
      ],
    ];
  }

}
