<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media_player\Hook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\Url;
use Drupal\Core\Extension\Requirement\RequirementSeverity;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Runtime requirements checks for FFmpeg Media Player.
 */
final class RequirementsHook {

  use StringTranslationTrait;

  /**
   * Constructs a RequirementsHook object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   * @param \Drupal\Core\Extension\ExtensionPathResolver $extensionPathResolver
   *   The extension path resolver.
   */
  public function __construct(
    protected readonly ConfigFactoryInterface $configFactory,
    protected readonly ExtensionPathResolver $extensionPathResolver,
  ) {}

  /**
   * Implements hook_runtime_requirements().
   *
   * @phpstan-return array<string, array<string, mixed>>
   */
  #[Hook('runtime_requirements')]
  public function runtimeRequirements(): array {
    $config = $this->configFactory->get('ffmpeg_media_player.settings');
    $playerSource = $config->get('player_source') ?? 'local';

    if ($playerSource !== 'local') {
      return [];
    }

    $modulePath = $this->extensionPathResolver->getPath('module', 'ffmpeg_media_player');
    $requiredAssets = [
      'js/vendor/vidstack/vidstack.js',
      'js/vendor/vidstack/libs/hls.js',
      'js/vendor/vidstack/libs/dash.js',
      'js/vendor/vidstack/base.css',
      'js/vendor/vidstack/theme.css',
      'js/vendor/vidstack/video.css',
    ];
    $missingAssets = array_values(array_filter(
      $requiredAssets,
      static fn(string $asset): bool => !file_exists($modulePath . '/' . $asset),
    ));

    $requirements = [
      'ffmpeg_media_player_vidstack_bundle' => [
        'title' => $this->t('FFmpeg Media Player: Vendored Vidstack base assets'),
      ],
      'ffmpeg_media_player_vidstack_runtime' => [
        'title' => $this->t('FFmpeg Media Player: Local bundle runtime behavior'),
        'value' => $this->t('Local runtime active'),
        'severity' => RequirementSeverity::OK,
        'description' => $this->t('Local player mode serves the Vidstack runtime, captions parser, and HLS/DASH helpers from this module. Google Cast is disabled in local mode to avoid third-party runtime requests.'),
      ],
    ];

    if ($missingAssets === []) {
      $requirements['ffmpeg_media_player_vidstack_bundle']['value'] = $this->t('Installed');
      $requirements['ffmpeg_media_player_vidstack_bundle']['severity'] = RequirementSeverity::OK;
      $requirements['ffmpeg_media_player_vidstack_bundle']['description'] = $this->t('Vendored runtime assets are present under <code>js/vendor/vidstack</code>.');
    }
    else {
      $requirements['ffmpeg_media_player_vidstack_bundle']['value'] = $this->t('Missing assets');
      $requirements['ffmpeg_media_player_vidstack_bundle']['severity'] = RequirementSeverity::Warning;
      $requirements['ffmpeg_media_player_vidstack_bundle']['description'] = $this->t('The vendored local runtime bundle is incomplete. Missing: <code>@assets</code>. Rebuild/copy the assets into <code>js/vendor/vidstack</code>, or switch to CDN mode in the <a href=":url">player settings</a>.', [
        '@assets' => implode(', ', $missingAssets),
        ':url' => Url::fromRoute('ffmpeg_media_player.settings')->toString(),
      ]);
      $requirements['ffmpeg_media_player_vidstack_runtime']['value'] = $this->t('Local runtime incomplete');
      $requirements['ffmpeg_media_player_vidstack_runtime']['severity'] = RequirementSeverity::Warning;
      $requirements['ffmpeg_media_player_vidstack_runtime']['description'] = $this->t('Local player mode is enabled, but required vendored runtime assets are missing. Local playback may fail until the bundle is rebuilt or CDN mode is selected.');
    }

    return $requirements;
  }

}
