<?php

namespace App\Base\Framework\Controller;

use App\Monitor\Action\Handler\MonitorTvShowHandler;
use App\Tmdb\Tmdb;
use App\User\Framework\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Attribute\Route;

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

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

    #[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!'
        ]);
    }
}
