<?php

namespace App\Discover\Framework\Controller;

use App\Base\Enum\MediaType;
use App\Tmdb\TmdbClient;
use App\Tmdb\TmdbMovieGenre;
use App\Tmdb\TmdbTvShowGenre;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/discover')]
class WebController extends AbstractController
{
    #[Route('/', name: 'app.discover')]
    public function index(TmdbClient $tmdb)
    {
        $movies = $tmdb->popularMovies(18);
        $tvshows = $tmdb->popularTvShows(18);

        return $this->render('discover/index.html.twig', [
            'movies' => $movies,
            'shows' => $tvshows,
        ]);
    }

    #[Route('/{mediaType}/{genreId?}', name: 'app.discover.browse')]
    public function browse(string $mediaType, ?string $genreId, TmdbClient $tmdb)
    {
        if (null === $genreId) {
            if (MediaType::tryFrom($mediaType) === null) {
                return new Response(status: 404);
            }
            return $this->render('discover/browse.html.twig', [
                'genres' => self::getGenres($mediaType),
                'media_type' => $mediaType,
            ]);
        }

        if (MediaType::tryFrom($mediaType) === null) {
            return new Response(status: 404);
        }

        if (TmdbMovieGenre::tryFrom($genreId) === null &&
            TmdbTvShowGenre::tryFrom($genreId) === null
        ) {
            return new Response(status: 404);
        }

        $results = match ($mediaType) {
            MediaType::Movie->value => $tmdb->discoverMovies(
                [TmdbMovieGenre::from($genreId)->value]
            ),
            MediaType::TvShow->value => $tmdb->discoverTvShows(
                [TmdbTvShowGenre::from($genreId)->value]
            ),
        };

        return $this->render('discover/browse_genre.html.twig', [
            'media' => $results,
            'genre' => TmdbMovieGenre::from($genreId)->name,
            'genre_id' => $genreId,
            'media_type' => $mediaType,
        ]);
    }

    #[Route('/{mediaType}/{genreId}', name: 'app.discover.browse_genre')]
    public function browseGenre(string $mediaType, string $genreId, TmdbClient $tmdb)
    {
        if (MediaType::tryFrom($mediaType) === null) {
            return new Response(status: 404);
        }

        if (TmdbMovieGenre::tryFrom($genreId) === null &&
            TmdbTvShowGenre::tryFrom($genreId) === null
        ) {
            return new Response(status: 404);
        }

        $results = match ($mediaType) {
            MediaType::Movie->value => $tmdb->discoverMovies(
                [TmdbMovieGenre::from($genreId)->value]
            ),
            MediaType::TvShow->value => $tmdb->discoverTvShows(
                [TmdbTvShowGenre::from($genreId)->value]
            ),
        };

        return $this->render('discover/browse.html.twig', [
            'media' => $results,
            'media_type' => $mediaType,
        ]);
    }

    private static function getGenres(string $mediaType): array
    {
        return match ($mediaType) {
            MediaType::Movie->value => [
                'Action' => TmdbMovieGenre::Action->value,
                'Adventure' => TmdbMovieGenre::Adventure->value,
                'Animation' => TmdbMovieGenre::Animation->value,
                'Comedy' => TmdbMovieGenre::Comedy->value,
                'Crime' => TmdbMovieGenre::Crime->value,
                'Documentary' => TmdbMovieGenre::Documentary->value,
                'Drama' => TmdbMovieGenre::Drama->value,
                'Family' => TmdbMovieGenre::Family->value,
                'Fantasy' => TmdbMovieGenre::Fantasy->value,
                'History' => TmdbMovieGenre::History->value,
                'Horror' => TmdbMovieGenre::Horror->value,
                'Music' => TmdbMovieGenre::Music->value,
                'Mystery' => TmdbMovieGenre::Mystery->value,
                'Romance' => TmdbMovieGenre::Romance->value,
                'Science Fiction' => TmdbMovieGenre::ScienceFiction->value,
                'TV Movie' => TmdbMovieGenre::TvMovie->value,
                'Thriller' => TmdbMovieGenre::Thriller->value,
                'War' => TmdbMovieGenre::War->value,
                'Western' => TmdbMovieGenre::Western->value,
            ],
            MediaType::TvShow->value => [
                'Action & Adventure' => TmdbTvShowGenre::ActionAndAdventure->value,
                'Animation' => TmdbTvShowGenre::Animation->value,
                'Comedy' => TmdbTvShowGenre::Comedy->value,
                'Crime' => TmdbTvShowGenre::Crime->value,
                'Documentary' => TmdbTvShowGenre::Documentary->value,
                'Drama' => TmdbTvShowGenre::Drama->value,
                'Family' => TmdbTvShowGenre::Family->value,
                'Kids' => TmdbTvShowGenre::Kids->value,
                'Mystery' => TmdbTvShowGenre::Mystery->value,
                'News' => TmdbTvShowGenre::News->value,
                'Reality' => TmdbTvShowGenre::Reality->value,
                'Sci-Fi & Fantasy' => TmdbTvShowGenre::SciFiAndFantasy->value,
                'Soap' => TmdbTvShowGenre::Soap->value,
                'Talk' => TmdbTvShowGenre::Talk->value,
                'War & Politics' => TmdbTvShowGenre::WarAndPolitics->value,
                'Western' => TmdbTvShowGenre::Western->value,
            ],
            default => [],
        };
    }
}
