<?php
require_once __DIR__ . '/config.php';

function sendEmail($to, $toName, $subject, $body) {
    // Gunakan PHP mail() dengan SMTP via cPanel
    // Untuk hosting cPanel, mail() sudah dikonfigurasi via Exim
    
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $headers .= "From: " . SMTP_NAME . " <" . SMTP_FROM . ">\r\n";
    $headers .= "Reply-To: " . SMTP_FROM . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";

    $result = mail($to, $subject, $body, $headers);
    return $result;
}

function sendActivationEmail($email, $nama, $token, $role) {
    $link = BASE_URL . '/kkn-daftar?action=aktivasi&token=' . $token;
    $roleLabel = $role === 'dpl' ? 'Dosen Pembimbing Lapangan (DPL)' : 'Mahasiswa';

    $body = '
    <!DOCTYPE html>
    <html>
    <head><meta charset="UTF-8"></head>
    <body style="font-family:\'Segoe UI\',sans-serif;background:#f5f7fa;margin:0;padding:20px;">
      <div style="max-width:560px;margin:0 auto;background:#fff;border-radius:12px;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,0.08);">
        <div style="background:linear-gradient(135deg,#0D2E5C,#1A56A4);padding:32px 28px;text-align:center;">
          <img src="' . BASE_URL . '/kkn-system/assets/logo-smart-kkn.png" 
               style="width:80px;height:80px;object-fit:contain;margin-bottom:10px;border-radius:10px;" alt="SMART-KKN">
          <h1 style="color:#fff;font-size:1.3rem;margin:0;font-weight:700;">SMART-KKN UMUS 2026</h1>
          <p style="color:rgba(255,255,255,0.7);font-size:0.78rem;margin:4px 0 0;">Sistem Manajemen Administrasi dan Registrasi Terpadu KKN</p>
        </div>
        <div style="padding:32px 28px;">
          <h2 style="color:#0D2E5C;font-size:1.1rem;margin:0 0 16px;">Aktivasi Akun ' . $roleLabel . '</h2>
          <p style="color:#444;line-height:1.7;margin:0 0 12px;">Assalamu\'alaikum <strong>' . htmlspecialchars($nama) . '</strong>,</p>
          <p style="color:#444;line-height:1.7;margin:0 0 24px;">
            Terima kasih telah mendaftar di Sistem KKN UMUS 2026. Klik tombol di bawah untuk mengaktifkan akun Anda:
          </p>
          <div style="text-align:center;margin:28px 0;">
            <a href="' . $link . '" style="background:#C8922A;color:#fff;font-weight:700;padding:14px 32px;border-radius:6px;text-decoration:none;font-size:0.95rem;display:inline-block;">
              ✅ Aktivasi Akun Saya
            </a>
          </div>
          <p style="color:#888;font-size:0.8rem;margin:0 0 8px;">Link aktivasi berlaku selama <strong>24 jam</strong>.</p>
          <p style="color:#888;font-size:0.8rem;margin:0;">Jika Anda tidak mendaftar, abaikan email ini.</p>
        </div>
        <div style="background:#f5f7fa;padding:16px 28px;text-align:center;border-top:1px solid #E2E8F0;">
          <p style="color:#aaa;font-size:0.75rem;margin:0;">
            © 2026 LPPM Universitas Muhadi Setiabudi · Brebes, Jawa Tengah<br>
            <a href="' . BASE_URL . '" style="color:#1A56A4;">lppm.umus.ac.id</a>
          </p>
        </div>
      </div>
    </body>
    </html>';

    return sendEmail($email, $nama, '✅ Aktivasi Akun KKN UMUS 2026', $body);
}

function sendCredentialEmail($email, $nama, $username, $password_plain, $role) {
    $roleLabel = $role === 'dpl' ? 'Dosen Pembimbing Lapangan' : 'Mahasiswa';
    $loginUrl  = BASE_URL . '/kkn-login';

    $body = '
    <!DOCTYPE html>
    <html>
    <head><meta charset="UTF-8"></head>
    <body style="font-family:\'Segoe UI\',sans-serif;background:#f5f7fa;margin:0;padding:20px;">
      <div style="max-width:560px;margin:0 auto;background:#fff;border-radius:12px;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,0.08);">
        <div style="background:linear-gradient(135deg,#0D2E5C,#1A56A4);padding:32px 28px;text-align:center;">
          <img src="' . BASE_URL . '/kkn-system/assets/logo-smart-kkn.png" 
               style="width:80px;height:80px;object-fit:contain;margin-bottom:10px;border-radius:10px;" alt="SMART-KKN">
          <h1 style="color:#fff;font-size:1.3rem;margin:0;">SMART-KKN · LPPM UMUS 2026</h1>
        </div>
        <div style="padding:32px 28px;">
          <h2 style="color:#0D2E5C;font-size:1.1rem;margin:0 0 16px;">🎉 Akun Anda Telah Aktif!</h2>
          <p style="color:#444;line-height:1.7;margin:0 0 24px;">
            Halo <strong>' . htmlspecialchars($nama) . '</strong>, akun ' . $roleLabel . ' Anda sudah aktif. Berikut informasi login:
          </p>
          <div style="background:#E8F0FB;border-radius:8px;padding:20px;margin-bottom:24px;">
            <table style="width:100%;font-size:0.9rem;">
              <tr>
                <td style="color:#666;padding:4px 0;width:120px;">Username</td>
                <td style="color:#0D2E5C;font-weight:700;">: ' . htmlspecialchars($username) . '</td>
              </tr>
              <tr>
                <td style="color:#666;padding:4px 0;">Password</td>
                <td style="color:#0D2E5C;font-weight:700;">: ' . htmlspecialchars($password_plain) . '</td>
              </tr>
              <tr>
                <td style="color:#666;padding:4px 0;">Role</td>
                <td style="color:#0D2E5C;font-weight:700;">: ' . $roleLabel . '</td>
              </tr>
            </table>
          </div>
          <p style="color:#e53e3e;font-size:0.82rem;margin:0 0 20px;">
            ⚠️ Segera ganti password setelah login pertama untuk keamanan akun Anda.
          </p>
          <div style="text-align:center;">
            <a href="' . $loginUrl . '" style="background:#0D2E5C;color:#fff;font-weight:700;padding:14px 32px;border-radius:6px;text-decoration:none;font-size:0.95rem;display:inline-block;">
              🔐 Login Sekarang
            </a>
          </div>
        </div>
        <div style="background:#f5f7fa;padding:16px 28px;text-align:center;border-top:1px solid #E2E8F0;">
          <p style="color:#aaa;font-size:0.75rem;margin:0;">
            © 2026 LPPM Universitas Muhadi Setiabudi · 
            <a href="' . BASE_URL . '" style="color:#1A56A4;">lppm.umus.ac.id</a>
          </p>
        </div>
      </div>
    </body>
    </html>';

    return sendEmail($email, $nama, '🔐 Informasi Akun KKN UMUS 2026', $body);
}
