<?php

namespace App\Services\Users;

use App\Services\EmailService;
use Illuminate\Support\Facades\Log;

class UserEventService
{
    public function __construct(private EmailService $emailService)
    {
    }

    public function handleNewAccountAdded(array $userData): bool
    {
        $email = (string) ($userData['email'] ?? '');
        if ($email === '') {
            Log::warning('UserEventService: missing email for welcome message.');
            return false;
        }

        $name = trim((string) (($userData['firstname'] ?? '') . ' ' . ($userData['lastname'] ?? '')));
        $safeName = e($name !== '' ? $name : 'there');

        $subject = 'Welcome to Al Rahma Sunday School!';
        $message = '<p>Hello ' . $safeName . ',</p>'
            . '<p>Your account has been created. Welcome to Al Rahma Sunday School.</p>';

        return $this->emailService->send($email, $subject, $message, 'registration');
    }
}
