<?php

namespace App\Monitor\Framework\Controller;

use App\Download\Action\Input\DeleteDownloadInput;
use App\Library\Action\Command\GetMediaFromLibraryCommand;
use App\Library\Action\Handler\GetMediaInfoFromLibraryHandler;
use App\Monitor\Action\Handler\AddMonitorHandler;
use App\Monitor\Action\Handler\DeleteMonitorHandler;
use App\Monitor\Action\Input\AddMonitorInput;
use App\Monitor\Action\Input\DeleteMonitorInput;
use App\Monitor\Framework\Entity\Monitor;
use App\Monitor\Framework\Repository\MonitorRepository;
use App\Search\Action\Command\GetMediaInfoCommand;
use App\Search\Action\Handler\GetMediaInfoHandler;
use App\Tmdb\TmdbClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Attribute\Route;
use Twig\Environment;

class WebController extends AbstractController
{
    public function __construct(
        #[Autowire(service: 'twig')]
        private readonly Environment $renderer,
    ) {}

    #[Route('/monitors', name: 'app.monitors', methods: ['GET'])]
    public function addMonitor()
    {
        return $this->render('monitor/index.html.twig');
    }

    #[Route('/monitors/upcoming-episodes', name: 'app.monitor.upcoming-episodes', methods: ['GET'])]
    public function upcomingEpisodes()
    {
        return $this->render('monitor/upcoming-episodes.html.twig');
    }

    #[Route('/monitors/{id}', name: 'app.monitor.view', methods: ['GET'])]
    public function viewMonitor(Monitor $monitor, GetMediaInfoHandler $getMediaInfoHandler, GetMediaInfoFromLibraryHandler $handler)
    {
        $media = $getMediaInfoHandler->handle(
            new GetMediaInfoCommand(
                imdbId: $monitor->getImdbId(),
                mediaType: 'tvshows',
            )
        );
        $libraryResult = $handler->handle(
            new GetMediaFromLibraryCommand(
                $this->getUser()->getId(),
                $media->media->mediaType,
                $media->media->imdbId,
                $media->media->title,
            )
        );

        return $this->render('monitor/view.html.twig', [
            'monitor' => $monitor,
            'results' => $media,
            'library' => $libraryResult
        ]);
    }
}
