<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Form\SettingsTab;

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

/**
 * Builds the queue settings tab for the FFmpeg Media settings form.
 *
 * @internal
 */
final class QueueSettingsBuilder {

  use StringTranslationTrait;

  /**
   * Builds the queue 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['queue'] = [
      '#type' => 'details',
      '#title' => $this->t('Queue'),
      '#description' => $this->t('Control how video conversions are queued and processed. Large files use a separate queue to avoid blocking smaller jobs.'),
      '#group' => 'settings_tabs',
      '#open' => FALSE,
    ];

    $form['queue']['queue_claim_timeout'] = [
      '#type' => 'number',
      '#title' => $this->t('Queue claim timeout (seconds)'),
      '#description' => $this->t('How long a conversion job is reserved for processing before another worker can pick it up. Increase if conversions regularly take longer than this.'),
      '#default_value' => $config->get('queue_claim_timeout'),
      '#min' => 60,
      '#max' => 7200,
      '#required' => TRUE,
    ];

    $form['queue']['large_file_threshold'] = [
      '#type' => 'number',
      '#title' => $this->t('Large file threshold (bytes)'),
      '#description' => $this->t('Files above this size are routed to a dedicated large-file queue so they do not block smaller, faster conversions. Default: 100 MB (104857600).'),
      '#default_value' => $config->get('large_file_threshold'),
      '#min' => 1048576,
      '#required' => TRUE,
    ];

    $form['queue']['max_concurrent'] = [
      '#type' => 'number',
      '#title' => $this->t('Maximum concurrent conversions'),
      '#description' => $this->t('Maximum parallel conversions. Higher values process faster but consume more CPU, memory, and disk I/O. Start with 2 and increase based on server capacity.'),
      '#default_value' => $config->get('max_concurrent'),
      '#min' => 1,
      '#max' => 10,
      '#required' => TRUE,
    ];

    $form['queue']['lock_timeout'] = [
      '#type' => 'number',
      '#title' => $this->t('Lock timeout (seconds)'),
      '#description' => $this->t('Maximum time a conversion lock prevents duplicate processing of the same video. Should be longer than the longest expected conversion.'),
      '#default_value' => $config->get('lock_timeout'),
      '#min' => 60,
      '#max' => 86400,
      '#required' => TRUE,
    ];

    $form['queue']['queue_verbose_logging'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Verbose queue lifecycle logging'),
      '#description' => $this->t('Enable debug logs for routine queue lifecycle events (skip, defer, start, success). Keep disabled in production unless troubleshooting.'),
      '#default_value' => (bool) ($config->get('queue_verbose_logging') ?? FALSE),
    ];
  }

}
