<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Plugin\FFmpegOutput;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ffmpeg_media\Attribute\FFmpegOutput;
use Drupal\ffmpeg_media\ValueObject\ConversionOptions;

/**
 * MKV output format plugin.
 */
#[FFmpegOutput(
  id: 'mkv',
  label: new TranslatableMarkup('MKV'),
  description: new TranslatableMarkup('Matroska container supporting all major codecs.'),
  extension: 'mkv',
  mimeType: 'video/x-matroska',
  supportedCodecs: ['h264', 'hevc', 'vp9', 'av1', 'ffv1'],
  defaultCodec: 'h264',
  weight: 20,
)]
final class MkvOutput extends FFmpegOutputBase {

  /**
   * {@inheritdoc}
   */
  #[\Override]
  #[\NoDiscard]
  public function buildArguments(ConversionOptions $options): array {
    return [
      '-f', 'matroska',
    ];
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  #[\NoDiscard]
  public function generateOutputPath(string $inputPath): string {
    $baseName = pathinfo($inputPath, PATHINFO_FILENAME) ?: 'video';
    $directory = pathinfo($inputPath, PATHINFO_DIRNAME) ?: '';

    return $directory . DIRECTORY_SEPARATOR . $baseName . '_converted.mkv';
  }

}
