<?php

namespace App\Services\Comms;

use App\Mail\ContactMessageMail;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

/**
 * Outbound email to contacts. Uses the app's configured mailer (set MAIL_* to
 * a Ladill Bird SMTP credential in production so mail leaves as the account's
 * sender). Failures are swallowed + logged; the caller decides what to record.
 */
class EmailService
{
    public function send(string $to, string $subject, string $body, ?string $fromName = null, ?string $replyTo = null): bool
    {
        if (! filter_var($to, FILTER_VALIDATE_EMAIL)) {
            return false;
        }

        try {
            Mail::to($to)->send(new ContactMessageMail($subject, $body, $fromName, $replyTo));

            return true;
        } catch (\Throwable $e) {
            Log::warning('CRM email send failed', ['to' => $to, 'error' => $e->getMessage()]);

            return false;
        }
    }
}
