<?php

declare(strict_types=1);

namespace Drupal\flowdrop_workflow\DTO;

use Drupal\flowdrop\DTO\ValidationResult;
use Drupal\flowdrop\Service\Bundle\VerificationResult;

/**
 * The outcome of importing (or previewing) a workflow bundle.
 *
 * Carries the signature verdict, the structural validation result, and the
 * capability manifest so a caller (HTTP endpoint, drush) can render a single
 * "asking permissions" preflight and map the outcome to a status code.
 *
 * @internal
 *   This class is not part of the public API and may change without notice.
 */
final class BundleImportResult {

  /**
   * The bundle envelope declares an unsupported format/version.
   */
  public const UNSUPPORTED_FORMAT = 'unsupported_format';

  /**
   * Untrusted/unsigned bundle and the untrusted override was not granted.
   */
  public const UNTRUSTED_BLOCKED = 'untrusted_blocked';

  /**
   * The flow failed structural validation (R1/R4/R5).
   */
  public const INVALID = 'invalid';

  /**
   * A required capability is missing (a provider module must be installed).
   */
  public const MISSING_CAPABILITY = 'missing_capability';

  /**
   * A workflow with the bundle's id already exists.
   */
  public const CONFLICT = 'conflict';

  /**
   * Dry run: the bundle would apply cleanly. Nothing was written.
   */
  public const PREFLIGHT_OK = 'preflight_ok';

  /**
   * The workflow was imported and saved.
   */
  public const APPLIED = 'applied';

  /**
   * Constructs a BundleImportResult.
   *
   * @param string $status
   *   One of the status constants.
   * @param \Drupal\flowdrop\Service\Bundle\VerificationResult $verification
   *   The signature verdict.
   * @param \Drupal\flowdrop\DTO\ValidationResult|null $validation
   *   The structural validation result, if validation ran.
   * @param \Drupal\flowdrop_workflow\DTO\WorkflowCapabilityManifest|null $manifest
   *   The capability manifest, if it was built.
   * @param string|null $workflowId
   *   The saved workflow id, when applied.
   * @param list<string> $generatedNodeTypes
   *   Node type ids minted during this import.
   */
  public function __construct(
    private readonly string $status,
    private readonly VerificationResult $verification,
    private readonly ?ValidationResult $validation = NULL,
    private readonly ?WorkflowCapabilityManifest $manifest = NULL,
    private readonly ?string $workflowId = NULL,
    private readonly array $generatedNodeTypes = [],
  ) {}

  /**
   * Gets the status.
   *
   * @return string
   *   One of the status constants.
   */
  public function getStatus(): string {
    return $this->status;
  }

  /**
   * Gets the signature verdict.
   *
   * @return \Drupal\flowdrop\Service\Bundle\VerificationResult
   *   The verification result.
   */
  public function getVerification(): VerificationResult {
    return $this->verification;
  }

  /**
   * Gets the structural validation result.
   *
   * @return \Drupal\flowdrop\DTO\ValidationResult|null
   *   The validation result, or NULL if validation did not run.
   */
  public function getValidation(): ?ValidationResult {
    return $this->validation;
  }

  /**
   * Gets the capability manifest.
   *
   * @return \Drupal\flowdrop_workflow\DTO\WorkflowCapabilityManifest|null
   *   The manifest, or NULL if it was not built.
   */
  public function getManifest(): ?WorkflowCapabilityManifest {
    return $this->manifest;
  }

  /**
   * Gets the saved workflow id.
   *
   * @return string|null
   *   The workflow id, or NULL when nothing was saved.
   */
  public function getWorkflowId(): ?string {
    return $this->workflowId;
  }

  /**
   * Gets the node type ids minted during this import.
   *
   * @return list<string>
   *   The generated node type ids.
   */
  public function getGeneratedNodeTypes(): array {
    return $this->generatedNodeTypes;
  }

  /**
   * Whether the import succeeded (saved).
   *
   * @return bool
   *   TRUE only for the applied state.
   */
  public function isApplied(): bool {
    return $this->status === self::APPLIED;
  }

}
