<?php

namespace App\Base\Framework\Controller;

use App\Monitor\Action\Command\MonitorTvShowCommand;
use App\Monitor\Action\Handler\MonitorTvShowHandler;
use App\Tmdb\TmdbClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Attribute\Route;

final class IndexController extends AbstractController
{
    public function __construct(
        private readonly TmdbClient $tmdb,
    ) {}

    #[Route('/', name: 'app_index')]
    public function index(Request $request): Response
    {
        return $this->render('index/index.html.twig', [
            'active_downloads' => $this->getUser()->getActiveDownloads(),
            'recent_downloads' => $this->getUser()->getDownloads(),
            'popular_movies' => $this->tmdb->popularMovies(),
            'popular_tvshows' => $this->tmdb->popularTvShows(),
        ]);
    }

    #[Route('/email')]
    public function sendEmail(MailerInterface $mailer): Response
    {
        $email = (new Email())
            ->to('brock@caldwell.digital')
            ->subject('Time for Symfony Mailer!')
            ->text('Sending emails is fun again!')
            ->html('<p>See Twig integration for better HTML integration!</p>');

        $mailer->send($email);

        return $this->json([
            'success' => true,
            'message' => 'Email sent!'
        ]);
    }

    #[Route('/test')]
    public function monitorTvShow(MonitorTvShowHandler $handler): Response
    {
        throw new \Exception('Test');
        return $this->render('index/test.html.twig', []);
    }
}
