<?php

namespace DMD\LaLigaApi\Service\Common;

use DMD\LaLigaApi\Entity\League;
use DMD\LaLigaApi\Entity\Team;
use DMD\LaLigaApi\Entity\User;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;

class EmailSender
{
    public function __construct(
        public MailerInterface $mailer
    )
    {
    }

    /**
     * @throws TransportExceptionInterface
     */
    public function newJoinLeagueRequest(User $leagueAdminEntity, User $requestingUserEntity, League $leagueEntity): void
    {
        $requestingUserFullName = $requestingUserEntity->getFirstName() . ' '. $requestingUserEntity->getLastName();
        $email = (new TemplatedEmail())
            ->from('soporteliga@dyb-tech.com')
            ->to(
                (new Address($leagueAdminEntity?->getEmail()))
            )
            ->subject("Nueva solicitud: $requestingUserFullName se quiere unir a ". $leagueEntity->getName())
            ->htmlTemplate('joinLeague.html.twig')
            ->context([
                'user' => $requestingUserEntity,
                'president'=> $leagueAdminEntity,
                'league' => $leagueEntity
            ]);
        $this->mailer->send($email);
    }

    /**
     * @throws TransportExceptionInterface
     */
    public function teamCaptainRequest(Team $teamEntity, User $leagueAdminEntity, User $requestingUser, League $leagueEntity): void
    {
        $requestingUserFullName = $requestingUser->getFirstName() .' '. $requestingUser->getLastName();
        $email = (new TemplatedEmail())
            ->from('soporteliga@dyb-tech.com')
            ->to(
                (new Address($leagueAdminEntity->getEmail()))
            )
            ->subject("Nueva solicitud: $requestingUserFullName se quiere unir a ". $leagueEntity->getName())
            ->htmlTemplate('teamCaptainRequest.html.twig')
            ->context([
                'userToNotify' => $leagueAdminEntity,
                'requestingUser'=> $requestingUser,
                'league'=> $leagueEntity,
                'team' => $teamEntity
            ]);
        $this->mailer->send($email);
    }
    /**
     * @throws TransportExceptionInterface
     */
    public function declineTeamCaptainRequest(User $rejectedUser, League $leagueEntity): void
    {
        $email = (new TemplatedEmail())
            ->from('soporte@leagueranks.es')
            ->to((new Address($rejectedUser->getEmail())))
            ->subject('Tu solicitud ha sido rechazada.')
            ->htmlTemplate('declinedRequest.html.twig')
            ->context([
                'rejectedUserEntity' => $rejectedUser,
                'leagueEntity'=> $leagueEntity
            ]);
        $this->mailer->send($email);
    }

    /**
     * @throws TransportExceptionInterface
     */
    public function joinLeagueRequestAccepted(User $user, League $league): void
    {
        $email = (new TemplatedEmail())
            ->from('soporte@leagueranks.es')
            ->to((new Address($user->getEmail())))
            ->subject('Tu solicitud ha sido aceptada.')
            ->htmlTemplate('welcomeToLeague.html.twig')
            ->context([
                'user' => $user,
                'leagueName'=> $league->getName()
            ]);
        $this->mailer->send($email);
    }
    /**
     * @throws TransportExceptionInterface
     */
    public function joinLeagueRequestDeclined(User $user, League $league): void
    {
        $email = (new TemplatedEmail())
            ->from('soporte@leagueranks.es')
            ->to((new Address($user->getEmail())))
            ->subject('Tu solicitud ha sido rechazada.')
            ->htmlTemplate('leagueRequestDeclined.html.twig')
            ->context([
                'user' => $user,
                'leagueName'=> $league->getName()
            ]);
        $this->mailer->send($email);
    }
}