<?php

declare(strict_types=1);

namespace Drupal\native_observability_cache_observer\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\native_observability_cache_observer\Service\CacheObserverStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Confirmation form used to delete all stored cache observer records.
 */
final class CacheObserverDeleteAllConfirmForm extends ConfirmFormBase {

  /**
   * Creates a new delete-all confirmation form instance.
   */
  public function __construct(
    private readonly CacheObserverStorage $cacheObserverStorage,
  ) {
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): static {
    return new static(
      $container->get(CacheObserverStorage::class),
    );
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getDescription(): TranslatableMarkup {
    return $this->t('This action removes stored observability records only. It does not clear Drupal runtime caches.');
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $deleted = $this->cacheObserverStorage->deleteAll();

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

    $form_state->setRedirect('native_observability_cache_observer.report');
  }

}
