<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\ValueObject;

/**
 * Immutable value object for a single output configuration in a profile.
 */
final readonly class ProfileOutput {

  /**
   * Constructs a ProfileOutput object.
   *
   * @param string $container
   *   Container format (e.g., 'mp4', 'webm', 'hls').
   * @param string $codec
   *   Codec plugin ID (e.g., 'h264', 'vp9', 'libjxl').
   * @param bool $enabled
   *   Whether this output is enabled.
   * @param array<string, mixed> $options
   *   Codec/conversion options for this output.
   *
   * @throws \InvalidArgumentException
   *   If container or codec is empty.
   */
  public function __construct(
    public string $container,
    public string $codec,
    public bool $enabled = TRUE,
    public array $options = [],
  ) {
    if ($container === '') {
      throw new \InvalidArgumentException('Container format cannot be empty.');
    }
    if ($codec === '') {
      throw new \InvalidArgumentException('Codec cannot be empty.');
    }
  }

  /**
   * Creates a ProfileOutput from a config array.
   *
   * @param array<string, mixed> $data
   *   Associative array with container, codec, enabled, and options keys.
   *
   * @return self
   *   New ProfileOutput instance.
   */
  #[\NoDiscard]
  public static function fromArray(array $data): self {
    return new self(
      container: (string) ($data['container'] ?? ''),
      codec: (string) ($data['codec'] ?? ''),
      enabled: (bool) ($data['enabled'] ?? TRUE),
      options: is_array($data['options'] ?? NULL) ? $data['options'] : [],
    );
  }

  /**
   * Converts to array format.
   *
   * @return array<string, bool|string|array<string, mixed>>
   *   Associative array representation.
   */
  #[\NoDiscard]
  public function toArray(): array {
    return [
      'container' => $this->container,
      'codec' => $this->codec,
      'enabled' => $this->enabled,
      'options' => $this->options,
    ];
  }

}
