<?php
// ============================================================
//  config/mail.php — PHPMailer via Composer (v7)
// ============================================================

// ─── SMTP DETAILS BHARO ──────────────────────────────────────
define('SMTP_HOST',      'smtp.gmail.com');   // e.g. mail.communeglobaljournals.in
define('SMTP_PORT',      587);                      // 465=SSL, 587=TLS
define('SMTP_SECURE',    'tls');                    // 'ssl' ya 'tls'
define('SMTP_USER',      'editorialoffice@rsis-international.org'); // cPanel email
define('SMTP_PASS',      'vbawhqzzusdgeahe');      // email password
define('SMTP_FROM_NAME', 'Commenueglobal');
define('APP_URL',        'https://communeglobal.com/reviewer'); // local test
// ─────────────────────────────────────────────────────────────

// Composer autoload (vendor folder project root mein hona chahiye)
$autoloadPaths = [
    __DIR__ . '/../vendor/autoload.php',
    __DIR__ . '/../../vendor/autoload.php',
];
$loaded = false;
foreach ($autoloadPaths as $p) {
    if (file_exists($p)) { require_once $p; $loaded = true; break; }
}
if (!$loaded) {
    // Fallback: manual PHPMailer files
    $manualPaths = [
        __DIR__ . '/../PHPMailer/src/Exception.php',
        __DIR__ . '/../PHPMailer/src/PHPMailer.php',
        __DIR__ . '/../PHPMailer/src/SMTP.php',
    ];
    foreach ($manualPaths as $mp) {
        if (file_exists($mp)) require_once $mp;
    }
}

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

/* ── Generic send function ── */
function sendMail(string $toEmail, string $toName, string $subject, string $htmlBody): bool {
    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();
        $mail->Host        = SMTP_HOST;
        $mail->SMTPAuth    = true;
        $mail->Username    = SMTP_USER;
        $mail->Password    = SMTP_PASS;
        $mail->SMTPSecure  = SMTP_SECURE;
        $mail->Port        = SMTP_PORT;
        $mail->CharSet     = 'UTF-8';
        $mail->SMTPOptions = [
            'ssl' => [
                'verify_peer'       => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true,
            ]
        ];

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

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $htmlBody;
        $mail->AltBody = strip_tags(str_replace(
            ['<br>','<br/>','</p>','</div>'], "\n", $htmlBody
        ));

        $mail->send();
        return true;
    } catch (Exception $e) {
        error_log('Mail error: ' . $mail->ErrorInfo);
        return false;
    }
}

/* ── Verification email ── */
function sendVerificationMail(string $email, string $name, string $token): bool {
    $link = APP_URL . '/verify_email.php?token=' . urlencode($token);
    $safeName = htmlspecialchars($name);
    $html = <<<HTML
    <div style="font-family:Arial,sans-serif;max-width:520px;margin:0 auto;border-radius:8px;overflow:hidden">
      <div style="background:#E05A2B;padding:22px 30px;text-align:center">
        <span style="font-size:24px;font-weight:700;color:#fff">🌿 communeglobal</span>
      </div>
      <div style="background:#fff;padding:32px 30px">
        <h2 style="color:#222;font-size:20px;margin:0 0 14px">Verify your email</h2>
        <p style="color:#555;font-size:14px;line-height:1.7;margin:0 0 24px">
          Hi <strong>{$safeName}</strong>,<br><br>
          Thank you for registering. Click the button below to activate your account.
        </p>
        <div style="text-align:center;margin-bottom:28px">
          <a href="{$link}" style="display:inline-block;padding:14px 36px;background:#1A7F8E;color:#fff;border-radius:6px;font-size:15px;font-weight:700;text-decoration:none">
            ✅ Verify Email Address
          </a>
        </div>
        <p style="color:#aaa;font-size:12px;line-height:1.6">
          Link expires in <strong>24 hours</strong>. If you didn't register, ignore this email.<br><br>
          Or copy: <span style="color:#1A7F8E;word-break:break-all">{$link}</span>
        </p>
      </div>
    </div>
HTML;
    return sendMail($email, $name, 'Verify your communeglobal account', $html);
}