<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\ValueObject;

/**
 * Immutable value object representing formatter-based conversion configuration.
 *
 * Configuration comes from EntityViewDisplay formatter settings.
 */
final readonly class ConversionContext {

  /**
   * Constructs a ConversionContext object.
   *
   * @param string $entityType
   *   The entity type (e.g., 'media').
   * @param string $bundle
   *   The bundle name (e.g., 'video').
   * @param string $fieldName
   *   The source field name containing the video file.
   * @param bool $autoConvert
   *   Whether to automatically convert on upload.
   * @param string|null $conversionProfile
   *   ID of conversion profile to use.
   *
   * @throws \InvalidArgumentException
   *   If required fields are empty.
   */
  public function __construct(
    public string $entityType,
    public string $bundle,
    public string $fieldName,
    public bool $autoConvert = TRUE,
    public ?string $conversionProfile = NULL,
  ) {
    if (empty($entityType)) {
      throw new \InvalidArgumentException('Entity type cannot be empty.');
    }
    if (empty($bundle)) {
      throw new \InvalidArgumentException('Bundle cannot be empty.');
    }
    if (empty($fieldName)) {
      throw new \InvalidArgumentException('Field name cannot be empty.');
    }
  }

  /**
   * Gets the context ID (entity_type.bundle.field_name).
   *
   * @return string
   *   The context identifier.
   */
  #[\NoDiscard]
  public function getId(): string {
    return $this->entityType . '.' . $this->bundle . '.' . $this->fieldName;
  }

  /**
   * Checks if this context applies to a specific entity and field.
   *
   * @param string $entityType
   *   Entity type to check.
   * @param string $bundle
   *   Bundle to check.
   * @param string $fieldName
   *   Field name to check.
   *
   * @return bool
   *   TRUE if context matches.
   */
  #[\NoDiscard]
  public function appliesTo(string $entityType, string $bundle, string $fieldName = ''): bool {
    if ($fieldName !== '') {
      return $this->entityType === $entityType && $this->bundle === $bundle && $this->fieldName === $fieldName;
    }
    return $this->entityType === $entityType && $this->bundle === $bundle;
  }

}
