<?php

namespace App\Infrastructure\Mail;

class Mailer
{
    public function __construct(private array $config)
    {
    }

    public function send(string $to, string $subject, string $body): bool
    {
        $dir = dirname(__DIR__, 3) . '/storage/logs';
        if (!is_dir($dir)) {
            @mkdir($dir, 0775, true);
        }

        return file_put_contents($dir . '/mail.log', json_encode([
            'to' => $to,
            'from' => $this->config['from'] ?? 'noreply@emmanylcode.local',
            'subject' => $subject,
            'body' => $body,
            'sent_at' => date('c'),
        ]) . PHP_EOL, FILE_APPEND) !== false;
    }
}
