/**
 * Email templates — server-side HTML strings.
 *
 * Kept inline (no React rendering) to stay fast in cron + serverless paths.
 * Each template returns { html, text, subject } so callers can pass straight
 * to sendEmail().
 *
 * Branding: warm dark mode (stone/amber) matching site theme. Inline CSS only.
 */

const SITE_NAME = process.env.NEXT_PUBLIC_SITE_NAME ?? "Tử Vi Số";
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? "https://tuvi.pain.io.vn";

function escape(s: string): string {
  return s
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#39;");
}

const baseStyle = `
  body { margin:0; padding:0; background:#0c0a09; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color:#e7e5e4; }
  .wrap { max-width:560px; margin:0 auto; padding:32px 16px; }
  .card { background:#1c1917; border:1px solid #292524; border-radius:16px; padding:32px; }
  h1 { font-family: Georgia, "Times New Roman", serif; color:#fafaf9; font-size:24px; margin:0 0 16px; }
  p { line-height:1.6; color:#d6d3d1; margin:0 0 12px; font-size:15px; }
  .btn { display:inline-block; background:#f59e0b; color:#1c1917; text-decoration:none; padding:12px 24px; border-radius:8px; font-weight:600; margin-top:16px; }
  .meta { color:#78716c; font-size:13px; margin-top:24px; border-top:1px solid #292524; padding-top:16px; }
  a { color:#fbbf24; }
`;

function shell(title: string, body: string): string {
  return `<!doctype html><html lang="vi"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escape(title)}</title><style>${baseStyle}</style></head><body><div class="wrap"><div class="card">${body}<div class="meta">${escape(SITE_NAME)} · <a href="${escape(SITE_URL)}">${escape(SITE_URL.replace(/^https?:\/\//, ""))}</a></div></div></div></body></html>`;
}

export type Rendered = { subject: string; html: string; text: string };

export function reminderEmail(opts: {
  userName: string;
  reminderTitle: string;
  reminderDate: Date;
  isLunar: boolean;
}): Rendered {
  const dateStr = opts.reminderDate.toLocaleDateString("vi-VN", {
    weekday: "long",
    day: "2-digit",
    month: "long",
    year: "numeric",
  });
  const lunarTag = opts.isLunar ? " (lịch âm)" : "";
  const subject = `Nhắc nhở: ${opts.reminderTitle}`;
  const html = shell(
    subject,
    `<h1>Nhắc nhở từ ${escape(SITE_NAME)}</h1>
     <p>Chào ${escape(opts.userName)},</p>
     <p>Đây là lời nhắc cho sự kiện <strong>${escape(opts.reminderTitle)}</strong>.</p>
     <p><strong>Ngày:</strong> ${escape(dateStr)}${escape(lunarTag)}</p>
     <a href="${escape(SITE_URL)}/me" class="btn">Quản lý nhắc nhở</a>`
  );
  const text = `Chào ${opts.userName},\n\nLời nhắc: ${opts.reminderTitle}\nNgày: ${dateStr}${lunarTag}\n\nQuản lý: ${SITE_URL}/me\n\n— ${SITE_NAME}`;
  return { subject, html, text };
}

export function welcomeEmail(opts: { userName: string; userEmail: string }): Rendered {
  const subject = `Chào mừng ${opts.userName} đến với ${SITE_NAME}`;
  const html = shell(
    subject,
    `<h1>Chào ${escape(opts.userName)}</h1>
     <p>Cảm ơn bạn đã đăng ký <strong>${escape(SITE_NAME)}</strong>. Tài khoản của bạn (${escape(opts.userEmail)}) đã sẵn sàng.</p>
     <p>Bạn có thể bắt đầu bằng cách lập lá số tử vi đầu tiên hoặc tra cứu phong thủy nhà.</p>
     <a href="${escape(SITE_URL)}/tu-vi-tron-doi" class="btn">Lập lá số ngay</a>
     <p style="margin-top:24px">Nếu có câu hỏi, trả lời lại email này hoặc xem trang <a href="${escape(SITE_URL)}/lien-he">Liên hệ</a>.</p>`
  );
  const text = `Chào ${opts.userName},\n\nCảm ơn bạn đã đăng ký ${SITE_NAME}. Tài khoản: ${opts.userEmail}\n\nLập lá số đầu tiên: ${SITE_URL}/tu-vi-tron-doi\n\n— ${SITE_NAME}`;
  return { subject, html, text };
}

export function passwordResetEmail(opts: {
  userName: string;
  resetUrl: string;
  expiresInHours: number;
}): Rendered {
  const subject = `Đặt lại mật khẩu — ${SITE_NAME}`;
  const html = shell(
    subject,
    `<h1>Đặt lại mật khẩu</h1>
     <p>Chào ${escape(opts.userName)},</p>
     <p>Bạn (hoặc ai đó) đã yêu cầu đặt lại mật khẩu cho tài khoản này. Nhấn nút bên dưới để tiếp tục — link sẽ hết hạn sau ${opts.expiresInHours} giờ.</p>
     <a href="${escape(opts.resetUrl)}" class="btn">Đặt lại mật khẩu</a>
     <p style="margin-top:24px">Nếu bạn không yêu cầu, hãy bỏ qua email này.</p>`
  );
  const text = `Đặt lại mật khẩu cho ${opts.userName}\n\nMở link sau (hết hạn ${opts.expiresInHours}h):\n${opts.resetUrl}\n\nNếu không phải bạn, bỏ qua email này.\n\n— ${SITE_NAME}`;
  return { subject, html, text };
}
