import { getSettings } from "@/lib/settings";

// Async statt eines reinen String-Templates, weil die Postanschrift unten im
// Footer live aus den Rechnungseinstellungen kommt (nicht hartkodiert) —
// eine echte Absenderadresse in jeder Mail ist ein starkes Vertrauenssignal
// für Spam-Filter (Gmail/Outlook werten eine Mail ohne jede postalische
// Angabe eher als Massen-/Spam-Mail) und bleibt so automatisch aktuell,
// falls sich die Geschäftsadresse mal ändert.
export async function emailShell({
  heading,
  bodyHtml,
  buttonLabel,
  buttonUrl,
  footerNote,
}: {
  heading: string;
  bodyHtml: string;
  buttonLabel: string;
  buttonUrl: string;
  footerNote: string;
}) {
  const settings = await getSettings();
  const inv = settings.invoiceSettings;
  const addressParts = [inv.businessName, inv.street, [inv.postCode, inv.city].filter(Boolean).join(" ")].filter(Boolean);
  const addressLine = addressParts.join(" · ");

  return `<!doctype html>
<html>
  <body style="margin:0;padding:0;background:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:32px 16px;">
      <tr>
        <td align="center">
          <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="max-width:480px;width:100%;background:#ffffff;border-radius:16px;overflow:hidden;border:1px solid #e5e5e5;">
            <tr>
              <td style="background:#101012;padding:30px 32px;text-align:center;">
                <span style="font-size:14px;letter-spacing:6px;color:#f5f5f4;text-transform:uppercase;font-weight:600;">Rakku Photography</span>
              </td>
            </tr>
            <tr>
              <td style="padding:36px 32px 8px;">
                <h1 style="margin:0 0 16px;font-size:19px;line-height:1.4;color:#101012;font-weight:600;">${heading}</h1>
                <div style="font-size:14px;line-height:1.6;color:#4b4b4d;">${bodyHtml}</div>
              </td>
            </tr>
            <tr>
              <td style="padding:8px 32px 36px;">
                <a href="${buttonUrl}" style="display:inline-block;background:#101012;color:#f5f5f4;text-decoration:none;font-size:13px;letter-spacing:1.5px;text-transform:uppercase;font-weight:600;padding:13px 28px;border-radius:8px;">${buttonLabel}</a>
                <p style="margin:20px 0 0;font-size:12px;color:#9c9c9e;word-break:break-all;">${buttonUrl}</p>
              </td>
            </tr>
            <tr>
              <td style="padding:20px 32px;border-top:1px solid #ececec;">
                <p style="margin:0;font-size:12px;color:#9c9c9e;">${footerNote}</p>
                ${addressLine ? `<p style="margin:8px 0 0;font-size:11px;color:#b5b5b7;">${addressLine}</p>` : ""}
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>`;
}
