<?php

declare(strict_types=1);

namespace Apexl\Io\Notifier\Drivers;

use Apexl\Io\Factories\MailerFactory;
use Apexl\Io\Notifier\Interfaces\EmailNotifierDriverInterface;
use PHPMailer\PHPMailer\Exception;

class SmtpEmailNotifierDriver implements EmailNotifierDriverInterface
{
    private bool $isHtml = true;

    public function __construct(private MailerFactory $mailerFactory) {}

    /**
     * @param array<array{path: string, name?: string}> $attachments
     * @param array<array{path: string, cid: string, name?: string}> $embeddedImages
     *
     * @throws Exception
     */
    public function send(string $to, string $subject, string $body, array $attachments = [], array $embeddedImages = []): void
    {
        $client = $this->mailerFactory->make();

        $client->addAddress($to);
        $client->Subject = $subject;
        $client->Body = $body;

        foreach ($embeddedImages as $image) {
            $client->addEmbeddedImage($image['path'], $image['cid'], $image['name'] ?? '');
        }

        foreach ($attachments as $attachment) {
            $client->addAttachment($attachment['path'], $attachment['name'] ?? '');
        }

        $client->SMTPSecure = '';
        $client->isHTML($this->isHtml);

        $client->send();
    }

    public function isHtml(bool $isHtml = true): void
    {
        $this->isHtml = $isHtml;
    }
}
