<?php

declare(strict_types=1);

namespace Drupal\flowdrop_interrupt\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\flowdrop_interrupt\Entity\FlowDropInterruptInterface;
use Drupal\flowdrop_interrupt\Service\InterruptManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Confirmation form for cancelling a pending interrupt.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class CancelInterruptForm extends ConfirmFormBase {

  /**
   * The interrupt being processed.
   *
   * @var \Drupal\flowdrop_interrupt\Entity\FlowDropInterruptInterface|null
   */
  private ?FlowDropInterruptInterface $interrupt = NULL;

  public function __construct(
    protected readonly InterruptManagerInterface $interruptManager,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): static {
    return new static($container->get('flowdrop_interrupt.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'flowdrop_interrupt_cancel';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion(): TranslatableMarkup {
    return $this->t('Cancel this interrupt?');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription(): TranslatableMarkup {
    $message = $this->interrupt?->getMessage() ?? '';
    return $message
      ? $this->t('Message: @message', ['@message' => $message])
      : $this->t('This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl(): Url {
    return Url::fromRoute('flowdrop_interrupt.inbox');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText(): TranslatableMarkup {
    return $this->t('Yes, cancel it');
  }

  /**
   * {@inheritdoc}
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $interrupt_id
   *   The interrupt UUID passed from the route.
   */
  public function buildForm(array $form, FormStateInterface $form_state, string $interrupt_id = ''): array {
    $this->interrupt = $this->interruptManager->getInterrupt($interrupt_id);

    if (!$this->interrupt || !$this->interrupt->isPending()) {
      return [
        'unavailable' => [
          '#markup' => '<p>' . $this->t('This interrupt is no longer pending.') . '</p>',
        ],
      ];
    }

    $form_state->set('interrupt_id', $interrupt_id);
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $interrupt_id = $form_state->get('interrupt_id');

    if ($this->interruptManager->cancelInterrupt($interrupt_id)) {
      $this->messenger()->addStatus($this->t('Interrupt cancelled.'));
    }
    else {
      $this->messenger()->addWarning($this->t('The interrupt could not be cancelled — it may have already been resolved.'));
    }

    $form_state->setRedirect('flowdrop_interrupt.inbox');
  }

}
