<?php

declare(strict_types=1);

namespace Drupal\native_observability_cache_observer\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configuration form for the cache observer submodule.
 */
final class CacheObserverSettingsForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames(): array {
    return ['native_observability_cache_observer.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config('native_observability_cache_observer.settings');

    $form['description'] = [
      '#type' => 'item',
      '#markup' => $this->t('Configure how the Cache Observer submodule captures Drupal cache headers, response cacheability metadata, and cache tag invalidation activity.'),
    ];

    $form['capture_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable cache observer capture'),
      '#default_value' => (bool) $config->get('capture_enabled'),
      '#description' => $this->t('Globally enables or disables this submodule without uninstalling it.'),
    ];

    $form['capture_response_headers'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Capture Drupal cache response headers'),
      '#default_value' => (bool) $config->get('capture_response_headers'),
      '#description' => $this->t('Stores values such as X-Drupal-Cache and X-Drupal-Dynamic-Cache when they are present on the response.'),
    ];

    $form['capture_response_cacheability'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Capture response cacheability metadata'),
      '#default_value' => (bool) $config->get('capture_response_cacheability'),
      '#description' => $this->t('Stores bubbled cache tags, cache contexts, and max-age for cacheable responses.'),
    ];

    $form['capture_tag_invalidations'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Capture cache tag invalidations'),
      '#default_value' => (bool) $config->get('capture_tag_invalidations'),
      '#description' => $this->t('Stores invalidated cache tags and correlates them with the current Native Observability request when available.'),
    ];

    $form['retention_hours'] = [
      '#type' => 'number',
      '#title' => $this->t('Retention period (hours)'),
      '#default_value' => (int) ($config->get('retention_hours') ?? 72),
      '#min' => 1,
      '#step' => 1,
      '#required' => TRUE,
      '#description' => $this->t('Records older than this retention threshold are deleted by the centralized Native Observability cleanup manager.'),
    ];

    $form['report_limit'] = [
      '#type' => 'number',
      '#title' => $this->t('Recent report row limit'),
      '#default_value' => (int) ($config->get('report_limit') ?? 100),
      '#min' => 10,
      '#step' => 10,
      '#required' => TRUE,
      '#description' => $this->t('Maximum number of recent cache observer events displayed in the report table.'),
    ];

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->configFactory->getEditable('native_observability_cache_observer.settings')
      ->set('capture_enabled', (bool) $form_state->getValue('capture_enabled'))
      ->set('capture_response_headers', (bool) $form_state->getValue('capture_response_headers'))
      ->set('capture_response_cacheability', (bool) $form_state->getValue('capture_response_cacheability'))
      ->set('capture_tag_invalidations', (bool) $form_state->getValue('capture_tag_invalidations'))
      ->set('retention_hours', (int) $form_state->getValue('retention_hours'))
      ->set('report_limit', (int) $form_state->getValue('report_limit'))
      ->save();

    parent::submitForm($form, $form_state);
  }

}
