<?php

declare(strict_types=1);

namespace App\Service\Mailer\Builder;

use App\Entity\Access\Access;
use App\Enum\Core\EmailSendingTypeEnum;
use App\Service\Mailer\Email;
use App\Service\Mailer\Model\MailerModelInterface;
use App\Service\Mailer\Model\NewStructureArtistPremiumTrialRequestValidationModel;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;

/**
 * Classe NewStructureTrialRequestValidationBuilder qui est chargé de construire l'Email de validation d'une demande d'essai de nouvelle structure.
 */
class NewStructureArtistPremiumTrialRequestValidationBuilder extends AbstractBuilder implements BuilderInterface
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly string $opentalentNoReplyEmailAddress,
    ) {
    }

    public function support(MailerModelInterface $mailerModel): bool
    {
        return $mailerModel instanceof NewStructureArtistPremiumTrialRequestValidationModel;
    }

    /**
     * @param NewStructureArtistPremiumTrialRequestValidationModel $mailerModel
     */
    public function build(MailerModelInterface $mailerModel): ArrayCollection
    {
        $author = $this->entityManager->getRepository(Access::class)->find($mailerModel->getSenderId());

        $context = [
            'token' => $mailerModel->getToken(),
            'representativeFirstName' => $mailerModel->getRepresentativeFirstName(),
            'representativeLastName' => $mailerModel->getRepresentativeLastName(),
            'structureName' => $mailerModel->getStructureName(),
            'validationUrl' => $mailerModel->getValidationUrl(),
        ];

        $content = $this->render('shop/new-structure-artist-premium-trial-validation', $context);

        $email = (new Email())
            ->setEmailEntity($this->buildEmailEntity('Validation de votre demande d\'essai', $author, $content))
            ->setContent($content)
            ->setFrom($this->opentalentNoReplyEmailAddress)
            ->setFromName('Opentalent');

        // Add recipient as a string (direct email address)
        $this->addRecipient($email, $mailerModel->getRepresentativeEmail(), EmailSendingTypeEnum::TO);

        return new ArrayCollection([$email]);
    }
}
