<?php

namespace App\Search\Action\Handler;

use App\Base\Enum\MediaType;
use App\Search\Action\Command\GetMediaInfoCommand;
use App\Search\Action\Result\GetMediaInfoResult;
use App\Tmdb\TmdbClient;
use App\Tmdb\TmdbResult;
use OneToMany\RichBundle\Contract\CommandInterface;
use OneToMany\RichBundle\Contract\HandlerInterface;
use OneToMany\RichBundle\Contract\ResultInterface;

/** @implements HandlerInterface<GetMediaInfoCommand, GetMediaInfoResult> */
class GetMediaInfoHandler implements HandlerInterface
{
    public function __construct(
        private readonly TmdbClient $tmdb,
    ) {}

    public function handle(CommandInterface $command): ResultInterface
    {
        $handlers = [
            MediaType::Movie->value => 'getMovieDetails',
            MediaType::TvShow->value => 'getTvshowDetails',
        ];
        $handler = $handlers[$command->mediaType];
        $media = $this->$handler($command);
        $relatedMedia = $this->tmdb->relatedMedia($media->tmdbId, $command->mediaType);

        return new GetMediaInfoResult($media, $relatedMedia, $command->season, $command->episode);
    }

    private function getMovieDetails(CommandInterface $command): TmdbResult
    {
        return $this->tmdb->movieDetails($command->imdbId);
    }

    private function getTvshowDetails(CommandInterface $command): TmdbResult
    {
        $media = $this->tmdb->tvshowDetails($command->imdbId);
        return $media;
    }
}
