/**
 * "A document was shared with you" email — sent to every notified user
 * right after an admin uploads + assigns a document to their role.
 * Same visual style as document-otp-email-template.ts.
 */

export interface DocumentSharedEmailContext {
  recipient_name: string;
  document_title: string;
  document_category: string;
  uploader_name: string;
  require_otp: boolean;
  has_password: boolean;
  portal_url: string;
}

export function buildDocumentSharedEmail(ctx: DocumentSharedEmailContext): {
  subject: string;
  html: string;
} {
  const subject = `New document shared with you: "${ctx.document_title}"`;

  const securityLine = [
    ctx.require_otp ? 'an email OTP code' : null,
    ctx.has_password ? 'a password' : null,
  ]
    .filter(Boolean)
    .join(' and ');

  const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${subject}</title>
</head>
<body style="margin:0;padding:0;background:#faf9fd;font-family:Inter,Arial,sans-serif;color:#26203a;">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#faf9fd;padding:32px 12px;">
    <tr>
      <td align="center">
        <table role="presentation" width="560" cellpadding="0" cellspacing="0"
               style="background:#ffffff;border:1px solid #eae5f1;border-radius:16px;overflow:hidden;">
          <!-- Header -->
          <tr>
            <td style="background:linear-gradient(120deg,#2a0f57,#4a0080 60%,#6d28d9);padding:20px 26px;color:#fff;">
              <div style="font-family:'Fraunces',Georgia,serif;font-size:18px;font-weight:600;">
                CertifyHub — Document Portal
              </div>
            </td>
          </tr>

          <!-- Body -->
          <tr>
            <td style="padding:28px 26px 8px;">
              <h2 style="font-family:'Fraunces',Georgia,serif;font-size:20px;color:#1a1033;margin:0 0 6px;">
                Hello ${escapeHtml(ctx.recipient_name)},
              </h2>
              <p style="font-size:14px;color:#4b4560;line-height:1.55;margin:0 0 18px;">
                <b>${escapeHtml(ctx.uploader_name)}</b> just shared a new document with you
                on the Document Portal.
              </p>

              <div style="background:#fbfaff;border:1.5px solid #eae5f1;border-radius:12px;padding:16px 18px;margin:0 0 18px;">
                <div style="font-size:15px;font-weight:700;color:#1a0440;margin-bottom:4px;">
                  ${escapeHtml(ctx.document_title)}
                </div>
                <div style="font-size:12px;color:#8b8397;">
                  ${escapeHtml(ctx.document_category)}
                </div>
              </div>

              ${
                securityLine
                  ? `<p style="font-size:13px;color:#4b4560;line-height:1.55;margin:0 0 18px;">
                       This document is protected — you'll be asked for <b>${securityLine}</b>
                       when you open it. Just click "Open" on the document and follow the prompts;
                       any OTP code will be emailed to you at that point.
                     </p>`
                  : `<p style="font-size:13px;color:#4b4560;line-height:1.55;margin:0 0 18px;">
                       No extra verification is needed — just open it from the portal.
                     </p>`
              }

              <div style="text-align:center;margin:22px 0;">
                <a href="${escapeAttr(ctx.portal_url)}"
                   style="display:inline-block;background:linear-gradient(135deg,#7c3aed,#4a0080);
                          color:#fff;text-decoration:none;font-size:14px;font-weight:700;
                          padding:12px 28px;border-radius:10px;">
                  Open Document Portal →
                </a>
              </div>
            </td>
          </tr>

          <!-- Footer -->
          <tr>
            <td style="padding:18px 26px 22px;border-top:1px solid #eae5f1;background:#faf9fd;">
              <p style="font-size:11px;color:#8b8397;margin:0;line-height:1.5;">
                You're receiving this because you belong to a role this document was assigned to.
                Every open, download, and failed attempt is logged for audit purposes.
              </p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>
  `.trim();

  return { subject, html };
}

// Small helpers — never trust user-supplied strings inside HTML.
function escapeHtml(s: string): string {
  return String(s ?? '')
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;');
}

function escapeAttr(s: string): string {
  return escapeHtml(s);
}
