<?php

namespace App\Model;

use App\Model\MailBody;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;



class Mailer
{
    const RECIPIENT = 'pawel.kowala@ecomnova.com';
    private MailerInterface $mailer;
    private MailBody $mailBody;
    public function __construct(MailerInterface $mailer, MailBody $mailBody)
    {
        $this->mailer = $mailer;
        $this->mailBody = $mailBody;
    }

    public function sendEmail(array $errors, $executionTime = null): void
    {
        $body = $this->mailBody->parseErrors($errors, $executionTime);
        $subject = $this->getSubject($errors);
        $email = (new Email())
            ->from('development@ecomnova.com')
            ->to(self::RECIPIENT)
            ->subject($subject)
            ->text($body)
            ->html('<p>' . $body . '</p>');

        $this->mailer->send($email);
    }

    private function getSubject(array $errors)
    {
        $date = date('Y-m-d H:i:s');
        if(count($errors) > 1) {
            return 'Price check for Appleyard failed on ' . $date;
        }else{
            return 'No price errors found on Appleyard on ' . $date;
        }
    }

    public function getMailBody(): \App\Model\MailBody
    {
        return $this->mailBody;
    }

    public function setMailBody(\App\Model\MailBody $mailBody): void
    {
        $this->mailBody = $mailBody;
    }

    /**
     * @param mixed $executionTime
     */
    public function setExecutionTime($executionTime): void
    {
        $this->executionTime = $executionTime;
    }
}