<?php

declare(strict_types=1);

namespace Drupal\native_observability_database_observer\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\native_observability_database_observer\Service\DatabaseObserverStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Confirmation form used to delete every stored database observer row.
 */
final class DatabaseObserverDeleteAllConfirmForm extends ConfirmFormBase {

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

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

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $deleted = $this->storage->deleteAll();
    $this->messenger()->addStatus($this->t('Deleted @count database observer records.', [
      '@count' => $deleted,
    ]));
    $form_state->setRedirect('native_observability_database_observer.report');
  }

}
