<?php

namespace App\Library\Action\Input;

use App\Library\Action\Command\GetMediaFromLibraryCommand;
use OneToMany\RichBundle\Attribute\SourceRequest;
use OneToMany\RichBundle\Contract\CommandInterface as C;
use OneToMany\RichBundle\Contract\InputInterface;

/**
 * @implements InputInterface<GetMediaInfoFromLibraryInput, GetMediaFromLibraryCommand>
 */
class GetMediaInfoFromLibraryInput implements InputInterface
{
    public function __construct(
        #[SourceRequest('imdbId', nullify: true)]
        public ?string $imdbId = null,
        #[SourceRequest('title', nullify: true)]
        public ?string $title = null,
        #[SourceRequest('season', nullify: true)]
        public ?string $season = null,
        #[SourceRequest('episode', nullify: true)]
        public ?string $episode = null,
    ) {}

    public function toCommand(): C
    {
        if (null === $this->imdbId && null === $this->title) {
            throw new \InvalidArgumentException('Either imdbId or title must be set', 400);
        }

        return new GetMediaFromLibraryCommand(
            imdbId: $this->imdbId,
            title: $this->title,
            season: $this->season,
            episode: $this->episode,
        );
    }
}
