<?php

declare(strict_types=1);

namespace Drupal\native_observability_spans\Form;

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

/**
 * Confirms deletion of spans for a specific trace.
 */
final class DeleteTraceSpansForm extends ConfirmFormBase {

  /**
   * The trace storage.
   */
  private TraceStorageInterface $traceStorage;

  /**
   * The span storage.
   */
  private SpanStorageInterface $spanStorage;

  /**
   * The trace id.
   */
  private int $traceId = 0;

  /**
   * The request id.
   */
  private string $requestId = '';

  /**
   * Constructs the form.
   */
  public function __construct(TraceStorageInterface $trace_storage, SpanStorageInterface $span_storage) {
    $this->traceStorage = $trace_storage;
    $this->spanStorage = $span_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('native_observability.trace_storage'),
      $container->get('native_observability_spans.span_storage'),
    );
  }

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion(): TranslatableMarkup {
    return $this->t('Are you sure you want to delete all spans for this trace?');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription(): TranslatableMarkup {
    return $this->t('This action deletes all spans associated with the current trace request id.');
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl(): Url {
    return Url::fromRoute('native_observability_spans.trace_spans', ['id' => $this->traceId]);
  }

  /**
   * Builds the form.
   */
  public function buildForm(array $form, FormStateInterface $form_state, ?int $id = NULL): array {
    $this->traceId = (int) $id;

    $trace = $this->traceStorage->load($this->traceId);
    if (!$trace) {
      $this->messenger()->addError($this->t('Trace not found.'));
      $form_state->setRedirect('native_observability.traces');
      return [];
    }

    $this->requestId = (string) ($trace['request_id'] ?? '');

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $deleted = 0;

    if ($this->requestId !== '') {
      $deleted = $this->spanStorage->deleteByRequestId($this->requestId);
    }

    $this->messenger()->addStatus($this->t('Deleted @count spans.', [
      '@count' => $deleted,
    ]));

    $form_state->setRedirect('native_observability.trace_view', [
      'id' => $this->traceId,
    ]);
  }

}
