<?php

declare(strict_types=1);

namespace Drupal\native_observability\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\native_observability\Service\TraceStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Drupal\native_observability\Event\NativeObservabilityEvents;
use Drupal\native_observability\Event\TracesDeletedEvent;

/**
 * Confirmation form for deleting all traces.
 */
final class TraceDeleteAllConfirmForm extends ConfirmFormBase {

  /**
   * The event dispatcher.
   */
  protected EventDispatcherInterface $eventDispatcher;

  /**
   * The trace storage.
   */
  protected TraceStorageInterface $storage;

  /**
   * Constructs the delete-all confirmation form.
   *
   * @param \Drupal\native_observability\Service\TraceStorageInterface $trace_storage
   *   The trace storage service.
   * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(
    TraceStorageInterface $trace_storage,
    EventDispatcherInterface $event_dispatcher,
  ) {
    $this->storage = $trace_storage;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * Creates the form instance.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   *
   * @return self
   *   The form instance.
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('native_observability.trace_storage'),
      $container->get('event_dispatcher'),
    );
  }

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion(): string {
    return (string) $this->t('Delete ALL stored traces?');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription(): string {
    return (string) $this->t('This action cannot be undone.');
  }

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

  /**
   * {@inheritdoc}
   */
  public function getConfirmText(): string {
    return (string) $this->t('Delete all');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    // Optional: show how many will be deleted (safe to compute).
    $total = $this->storage->count([]);
    $deleted = $this->storage->deleteAll();

    // Dispatch an event so optional submodules can react to the trace purge.
    $this->eventDispatcher->dispatch(
      new TracesDeletedEvent((int) ($deleted ?: $total)),
      NativeObservabilityEvents::TRACES_DELETED
    );

    $this->messenger()->addStatus($this->t('Deleted @count trace(s).', [
      '@count' => $deleted ?: $total,
    ]));

    $form_state->setRedirect('native_observability.traces');
  }

}
