<?php
// mailer.php — Shared SMTP mailer for Dabil (used by send-otp.php, etc.)
//
// Requires PHPMailer. Install it with Composer:
//     composer require phpmailer/phpmailer
// then make sure vendor/autoload.php is required somewhere before this
// file runs (or require it directly below).
//
// If you don't have Composer access on your hosting account, download
// PHPMailer from https://github.com/PHPMailer/PHPMailer, upload the
// `src/` folder to your server (e.g. /PHPMailer/src), and replace the
// require line below with the three PHPMailer source files:
//
//   require __DIR__ . '/PHPMailer/src/Exception.php';
//   require __DIR__ . '/PHPMailer/src/PHPMailer.php';
//   require __DIR__ . '/PHPMailer/src/SMTP.php';

require_once __DIR__ . '/vendor/autoload.php';

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

// ------------------------------------------------------------------
// SMTP credentials for the mailbox that sends OTP emails.
// Update SMTP_HOST if your mail server uses a different hostname
// (check cPanel -> Email Accounts -> Connect Devices for the exact
// "Outgoing Server" / SMTP hostname and port).
// ------------------------------------------------------------------
define('SMTP_HOST', 'mail.dabil.com.ng');
define('SMTP_PORT', 465);              // 465 = SSL, 587 = STARTTLS
define('SMTP_SECURE', PHPMailer::ENCRYPTION_SMTPS); // use ENCRYPTION_STARTTLS for port 587
define('SMTP_USERNAME', 'support@dabil.com.ng');
define('SMTP_PASSWORD', 'Dabil@#@2026');
define('SMTP_FROM_EMAIL', 'support@dabil.com.ng');
define('SMTP_FROM_NAME', 'Dabil');

/**
 * Send an HTML email via SMTP.
 *
 * @param string $toEmail
 * @param string $toName
 * @param string $subject
 * @param string $htmlBody
 * @return array ['success' => bool, 'message' => string]
 */
function send_mail(string $toEmail, string $toName, string $subject, string $htmlBody): array
{
    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host       = SMTP_HOST;
        $mail->SMTPAuth   = true;
        $mail->Username   = SMTP_USERNAME;
        $mail->Password   = SMTP_PASSWORD;
        $mail->SMTPSecure = SMTP_SECURE;
        $mail->Port       = SMTP_PORT;

        $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
        $mail->addAddress($toEmail, $toName);
        $mail->addReplyTo(SMTP_FROM_EMAIL, SMTP_FROM_NAME);

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $htmlBody;
        $mail->AltBody  = strip_tags($htmlBody);

        $mail->send();
        return ['success' => true, 'message' => 'Email sent.'];
    } catch (Exception $e) {
        return ['success' => false, 'message' => 'Mailer error: ' . $mail->ErrorInfo];
    }
}

/**
 * Build the HTML body for an OTP email.
 */
function otp_email_body(string $name, string $otpCode, int $expiryMinutes): string
{
    $safeName = htmlspecialchars($name ?: 'there', ENT_QUOTES, 'UTF-8');

    return <<<HTML
    <div style="font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif; max-width:480px; margin:0 auto; padding:24px; color:#0F172A;">
      <h2 style="color:#2563EB; margin-bottom:4px;">Dabil</h2>
      <p>Hi {$safeName},</p>
      <p>Your verification code is:</p>
      <div style="font-size:32px; font-weight:800; letter-spacing:8px; background:#F0F4FF; border-radius:12px; padding:16px 24px; text-align:center; margin:16px 0;">
        {$otpCode}
      </div>
      <p>This code expires in {$expiryMinutes} minutes. If you didn't request this, you can safely ignore this email.</p>
      <p style="color:#64748B; font-size:13px; margin-top:24px;">— The Dabil Team</p>
    </div>
    HTML;
}
