import { env } from "@kk/env/server";
import nodemailer from "nodemailer";

// Re-use the same SMTP transport strategy as email.ts.
let _transport: nodemailer.Transporter | null | undefined;

function getTransport(): nodemailer.Transporter | null {
	if (_transport !== undefined) return _transport;
	if (!env.SMTP_HOST || !env.SMTP_USER || !env.SMTP_PASS) {
		_transport = null;
		return null;
	}
	_transport = nodemailer.createTransport({
		host: env.SMTP_HOST,
		port: env.SMTP_PORT,
		secure: env.SMTP_PORT === 465,
		auth: {
			user: env.SMTP_USER,
			pass: env.SMTP_PASS,
		},
	});
	return _transport;
}

const from = env.SMTP_FROM ?? "Kunstenkamp <info@kunstenkamp.be>";
const baseUrl = env.BETTER_AUTH_URL ?? "https://kunstenkamp.be";

function formatEuro(cents: number): string {
	return new Intl.NumberFormat("nl-BE", {
		style: "currency",
		currency: "EUR",
	}).format(cents / 100);
}

function deductionHtml(params: {
	firstName: string;
	amountCents: number;
	newBalanceCents: number;
	dateTime: string;
	drinkkaartUrl: string;
}) {
	return `<!DOCTYPE html>
<html lang="nl">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Drinkkaart afschrijving</title>
</head>
<body style="margin:0;padding:0;background:#f4f4f5;font-family:sans-serif;">
  <table width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f5;padding:40px 0;">
    <tr>
      <td align="center">
        <table width="600" cellpadding="0" cellspacing="0" style="background:#214e51;border-radius:4px;overflow:hidden;">
          <!-- Header -->
          <tr>
            <td style="padding:40px 48px 32px;">
              <p style="margin:0;font-size:13px;color:rgba(255,255,255,0.5);letter-spacing:0.08em;text-transform:uppercase;">Kunstenkamp</p>
              <h1 style="margin:12px 0 0;font-size:28px;color:#ffffff;font-weight:700;">Drinkkaart afschrijving</h1>
            </td>
          </tr>
          <!-- Body -->
          <tr>
            <td style="padding:0 48px 32px;">
              <p style="margin:0 0 16px;font-size:16px;color:rgba(255,255,255,0.85);line-height:1.6;">
                Hoi ${params.firstName},
              </p>
              <p style="margin:0 0 24px;font-size:16px;color:rgba(255,255,255,0.85);line-height:1.6;">
                Er is zojuist een bedrag afgeschreven van je Drinkkaart.
              </p>
              <!-- Summary -->
              <table width="100%" cellpadding="0" cellspacing="0" style="background:rgba(255,255,255,0.08);border-radius:4px;margin:0 0 24px;">
                <tr>
                  <td style="padding:20px 24px;">
                    <p style="margin:0 0 12px;font-size:12px;color:rgba(255,255,255,0.5);text-transform:uppercase;letter-spacing:0.06em;">Afschrijving</p>
                    <p style="margin:0 0 8px;font-size:22px;color:#ffffff;font-weight:700;">&minus; ${formatEuro(params.amountCents)}</p>
                    <p style="margin:8px 0 0;font-size:14px;color:rgba(255,255,255,0.55);">${params.dateTime}</p>
                  </td>
                </tr>
                <tr>
                  <td style="padding:0 24px 20px;border-top:1px solid rgba(255,255,255,0.08);">
                    <p style="margin:12px 0 4px;font-size:12px;color:rgba(255,255,255,0.5);text-transform:uppercase;letter-spacing:0.06em;">Nieuw saldo</p>
                    <p style="margin:0;font-size:20px;color:#ffffff;font-weight:600;">${formatEuro(params.newBalanceCents)}</p>
                  </td>
                </tr>
              </table>
              <!-- CTA -->
              <table cellpadding="0" cellspacing="0">
                <tr>
                  <td style="border-radius:2px;background:#ffffff;">
                    <a href="${params.drinkkaartUrl}" style="display:inline-block;padding:14px 32px;font-size:16px;font-weight:600;color:#214e51;text-decoration:none;">
                      Bekijk mijn Drinkkaart
                    </a>
                  </td>
                </tr>
              </table>
              <p style="margin:16px 0 0;font-size:13px;color:rgba(255,255,255,0.4);">
                Klopt er iets niet? Neem contact op via <a href="mailto:info@kunstenkamp.be" style="color:rgba(255,255,255,0.6);">info@kunstenkamp.be</a>.
              </p>
            </td>
          </tr>
          <!-- Footer -->
          <tr>
            <td style="padding:24px 48px;border-top:1px solid rgba(255,255,255,0.1);">
              <p style="margin:0;font-size:13px;color:rgba(255,255,255,0.4);line-height:1.6;">
                Kunstenkamp vzw — Een initiatief voor en door kunstenaars.
              </p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>`;
}

/**
 * Send a deduction notification email to the cardholder.
 * Fire-and-forget: errors are logged but not re-thrown.
 */
export async function sendDeductionEmail(params: {
	to: string;
	firstName: string;
	amountCents: number;
	newBalanceCents: number;
}): Promise<void> {
	const transport = getTransport();
	if (!transport) {
		console.warn("SMTP not configured — skipping deduction email");
		return;
	}

	const now = new Date();
	const dateTime = now.toLocaleString("nl-BE", {
		day: "2-digit",
		month: "2-digit",
		year: "numeric",
		hour: "2-digit",
		minute: "2-digit",
	});

	const amountFormatted = new Intl.NumberFormat("nl-BE", {
		style: "currency",
		currency: "EUR",
	}).format(params.amountCents / 100);

	await transport.sendMail({
		from,
		to: params.to,
		subject: `Drinkkaart — ${amountFormatted} afgeschreven`,
		html: deductionHtml({
			firstName: params.firstName,
			amountCents: params.amountCents,
			newBalanceCents: params.newBalanceCents,
			dateTime,
			drinkkaartUrl: `${baseUrl}/drinkkaart`,
		}),
	});
}
