<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Form\SettingsTab;

use Drupal\Core\Config\Config;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Builds the retry and recovery tab for the FFmpeg Media settings form.
 *
 * @internal
 */
final class RetrySettingsBuilder {

  use StringTranslationTrait;

  /**
   * Builds the retry section render array.
   *
   * @param array<string, mixed> $form
   *   Form structure, passed by reference.
   * @param \Drupal\Core\Config\Config $config
   *   Module configuration.
   */
  public function build(array &$form, Config $config): void {
    $form['retry'] = [
      '#type' => 'details',
      '#title' => $this->t('Retry & Recovery'),
      '#description' => $this->t('Automatic retry for failed conversions with exponential backoff and circuit breaker protection to prevent cascading failures.'),
      '#group' => 'settings_tabs',
      '#open' => FALSE,
    ];

    $form['retry']['retry_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable retry on failure'),
      '#description' => $this->t('Automatically retry conversions that fail due to temporary issues (e.g., resource limits, timeouts). Retries use increasing delays between attempts.'),
      '#default_value' => $config->get('retry.enabled'),
    ];

    $form['retry']['retry_max_retries'] = [
      '#type' => 'number',
      '#title' => $this->t('Maximum retries'),
      '#description' => $this->t('How many times to retry a failed conversion before marking it as permanently failed.'),
      '#default_value' => $config->get('retry.max_retries'),
      '#min' => 1,
      '#max' => 10,
      '#states' => [
        'visible' => [
          ':input[name="retry_enabled"]' => ['checked' => TRUE],
        ],
      ],
    ];

    $form['retry']['retry_base_delay'] = [
      '#type' => 'number',
      '#title' => $this->t('Base delay (seconds)'),
      '#description' => $this->t('Initial wait time before the first retry. Each subsequent retry doubles the delay (e.g., 300s, 600s, 1200s).'),
      '#default_value' => $config->get('retry.base_delay'),
      '#min' => 1,
      '#max' => 3600,
      '#states' => [
        'visible' => [
          ':input[name="retry_enabled"]' => ['checked' => TRUE],
        ],
      ],
    ];

    $form['retry']['retry_max_delay'] = [
      '#type' => 'number',
      '#title' => $this->t('Maximum delay (seconds)'),
      '#description' => $this->t('Upper limit for retry delay. Prevents exponential backoff from growing indefinitely.'),
      '#default_value' => $config->get('retry.max_delay'),
      '#min' => 1,
      '#max' => 86400,
      '#states' => [
        'visible' => [
          ':input[name="retry_enabled"]' => ['checked' => TRUE],
        ],
      ],
    ];

    // Circuit Breaker.
    $form['retry']['circuit_breaker'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Circuit Breaker'),
      '#description' => $this->t('Temporarily pauses all conversions when too many fail in a row, preventing system overload. Processing resumes automatically after the timeout period.'),
    ];

    $form['retry']['circuit_breaker']['circuit_breaker_failure_threshold'] = [
      '#type' => 'number',
      '#title' => $this->t('Failure threshold'),
      '#description' => $this->t('How many consecutive failures trigger the circuit breaker to pause all conversions.'),
      '#default_value' => $config->get('circuit_breaker.failure_threshold'),
      '#min' => 1,
      '#max' => 20,
    ];

    $form['retry']['circuit_breaker']['circuit_breaker_success_threshold'] = [
      '#type' => 'number',
      '#title' => $this->t('Success threshold'),
      '#description' => $this->t('How many successful test conversions are needed to fully resume normal processing after a circuit breaker trip.'),
      '#default_value' => $config->get('circuit_breaker.success_threshold'),
      '#min' => 1,
      '#max' => 10,
    ];

    $form['retry']['circuit_breaker']['circuit_breaker_timeout'] = [
      '#type' => 'number',
      '#title' => $this->t('Timeout (seconds)'),
      '#description' => $this->t('How long to wait after a circuit breaker trip before attempting a test conversion to check if the issue is resolved.'),
      '#default_value' => $config->get('circuit_breaker.timeout'),
      '#min' => 60,
      '#max' => 3600,
    ];
  }

}
