<?php
declare(strict_types=1);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

function mailer(): PHPMailer {
    $mail = new PHPMailer(true);

    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = false;
    $mail->Port = 25;
    $mail->CharSet = 'UTF-8';

    $mail->setFrom('no-reply@hamdanlegalgroup.ae', 'HLG Justice Hub');

    return $mail;
}

function send_salary_notification_to_employee(
    string $toEmail,
    string $employeeName,
    string $payrollMonth,
    float $netSalary,
    float $grossSalary = 0.0
): bool {

    if ($toEmail === '') return false;

    try {
        $mail = mailer();

        $mail->addAddress($toEmail, $employeeName);
        $mail->addCC('contact@hamdanlegalgroup.ae');

        $mail->isHTML(true);
        $mail->Subject = 'إشعار تحويل الراتب';

        $netFormatted   = number_format($netSalary, 2);
        $grossFormatted = number_format($grossSalary, 2);

        $mail->Body = "
        <div dir='rtl'>
            تم اعتماد راتب شهر {$payrollMonth}<br>
            صافي الراتب: {$netFormatted} درهم
        </div>";

        return $mail->send();

    } catch (Exception $e) {
        error_log($e->getMessage());
        return false;
    }
}