<?php

declare(strict_types=1);

namespace Drupal\flowdrop_trigger\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\flowdrop_orchestration\Service\Orchestrator\OrchestratorHelperInterface;
use Drupal\flowdrop_trigger\Enum\EventCategory;
use Drupal\flowdrop_trigger\Enum\TriggerExecutionMode;

/**
 * Defines a FlowDrop Event Type plugin attribute.
 *
 * Event type plugins define the different types of events that can
 * trigger workflow execution (e.g., entity.insert, user.login).
 *
 * @see \Drupal\flowdrop_trigger\Service\EventTypePluginManager
 * @see \Drupal\flowdrop_trigger\FlowDropEventTypeInterface
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class FlowDropEventType extends Plugin {

  /**
   * The event category.
   */
  public readonly EventCategory $category;

  /**
   * The default execution mode.
   */
  public readonly TriggerExecutionMode $defaultExecutionMode;

  /**
   * Constructs a FlowDropEventType attribute.
   *
   * @param string $id
   *   The plugin ID (e.g., "entity.insert", "user.login").
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable label for the event type.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $description
   *   A description of when this event fires.
   * @param \Drupal\flowdrop_trigger\Enum\EventCategory|string $category
   *   The event category.
   * @param string $defaultOrchestrator
   *   Default orchestrator type (flowdrop_runtime:synchronous, etc.).
   * @param \Drupal\flowdrop_trigger\Enum\TriggerExecutionMode|string $defaultExecutionMode
   *   Default execution mode.
   * @param array $supportedEntityTypes
   *   Supported entity types (empty array = all entity types).
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly TranslatableMarkup $description,
    EventCategory|string $category = EventCategory::Entity,
    public readonly string $defaultOrchestrator = OrchestratorHelperInterface::ASYNCHRONOUS,
    TriggerExecutionMode|string $defaultExecutionMode = TriggerExecutionMode::Async,
    public readonly array $supportedEntityTypes = [],
  ) {
    $this->category = $category instanceof EventCategory
      ? $category
      : EventCategory::from($category);
    $this->defaultExecutionMode = $defaultExecutionMode instanceof TriggerExecutionMode
      ? $defaultExecutionMode
      : TriggerExecutionMode::from($defaultExecutionMode);
  }

}
