<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\media\MediaInterface;

/**
 * Event dispatched when a video conversion fails.
 */
final class ConversionFailedEvent extends Event {

  /**
   * Constructs a ConversionFailedEvent.
   *
   * @param \Drupal\media\MediaInterface $media
   *   The media entity that failed conversion.
   * @param string $error
   *   Error message describing the failure.
   * @param \Throwable|null $exception
   *   The exception that caused the failure, if any.
   * @param string|null $currentStep
   *   The step that was being executed when failure occurred.
   */
  public function __construct(
    public readonly MediaInterface $media,
    public readonly string $error,
    public readonly ?\Throwable $exception = NULL,
    public readonly ?string $currentStep = NULL,
  ) {}

  /**
   * Gets the media entity ID.
   *
   * @return int
   *   The media entity ID.
   */
  public function getMediaId(): int {
    return (int) $this->media->id();
  }

  /**
   * Checks if the failure was caused by an exception.
   *
   * @return bool
   *   TRUE if an exception caused the failure.
   */
  public function hasException(): bool {
    return $this->exception !== NULL;
  }

  /**
   * Gets the exception class name if available.
   *
   * @return string|null
   *   Exception class name or NULL.
   */
  public function getExceptionClass(): ?string {
    return $this->exception !== NULL ? $this->exception::class : NULL;
  }

}
