<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media_ui\Hook;

use Drupal\Core\Hook\Attribute\Hook;

/**
 * Theme-related hook implementations for FFmpeg Media UI.
 */
final class ThemeHooks {

  /**
   * Implements hook_theme().
   *
   * @phpstan-return array<string, mixed>
   */
  #[Hook('theme')]
  public function theme(): array {
    return [
      'ffmpeg_media_ui_dashboard' => [
        'variables' => [
          'stats' => [],
          'queue_status' => [],
          'stats_cards' => NULL,
          'stats_24h' => NULL,
          'status_filter' => NULL,
          'media_table' => NULL,
          'pager' => NULL,
          'enhanced_queue_status' => NULL,
          'setup_alert' => NULL,
          'show_queue_editor' => FALSE,
          'show_operations' => FALSE,
          'ffmpeg_status' => NULL,
          'system_status' => NULL,
          'active_conversions' => [],
          'recent_conversions' => [],
          'recent_failures' => [],
          'quick_actions' => [],
          'live_refresh_enabled' => TRUE,
        ],
      ],
      'ffmpeg_media_video_preview' => [
        'variables' => [
          'media' => NULL,
          'source_url' => NULL,
          'poster_url' => NULL,
          'outputs' => [],
          'show_controls' => TRUE,
        ],
      ],
      'ffmpeg_media_progress_bar' => [
        'variables' => [
          'progress' => 0,
          'status' => 'pending',
          'message' => NULL,
          'animated' => TRUE,
        ],
      ],
      'ffmpeg_media_output_list' => [
        'variables' => [
          'outputs' => [],
          'show_details' => TRUE,
        ],
      ],
    ];
  }

  /**
   * Implements hook_preprocess_HOOK() for ffmpeg_media_progress_bar.
   *
   * @phpstan-param array<string, mixed> $variables
   */
  #[Hook('preprocess_ffmpeg_media_progress_bar')]
  public function preprocessProgressBar(array &$variables): void {
    $status = $variables['status'];
    $progress = (int) $variables['progress'];

    // Map status to CSS classes.
    // 'converting' is internal status, 'processing' is UI-friendly alias.
    $statusClasses = [
      'none' => 'ffmpeg-progress--none',
      'pending' => 'ffmpeg-progress--pending',
      'processing' => 'ffmpeg-progress--processing',
      'converting' => 'ffmpeg-progress--processing',
      'completed' => 'ffmpeg-progress--completed',
      'failed' => 'ffmpeg-progress--failed',
    ];

    $variables['status_class'] = $statusClasses[$status] ?? 'ffmpeg-progress--pending';
    $variables['progress_width'] = min(100, max(0, $progress));

    // Ensure progress is 100% for completed status.
    if ($status === 'completed') {
      $variables['progress_width'] = 100;
    }
  }

}
