import nodemailer from "nodemailer"

// Lazy-initialized SMTP transport (Mailcow)
let transporter: nodemailer.Transporter | null = null

function getTransporter() {
  if (!transporter && process.env.SMTP_PASS) {
    transporter = nodemailer.createTransport({
      host: process.env.SMTP_HOST || "mail.rmail.online",
      port: parseInt(process.env.SMTP_PORT || "587"),
      secure: false,
      auth: {
        user: process.env.SMTP_USER || "newsletter@cryptocommonsgather.ing",
        pass: process.env.SMTP_PASS,
      },
      tls: { rejectUnauthorized: false },
    })
  }
  return transporter
}

const EMAIL_FROM =
  process.env.EMAIL_FROM ||
  "Crypto Commons Gathering <newsletter@cryptocommonsgather.ing>"

const INTERNAL_NOTIFY_EMAIL = "contact@cryptocommonsgather.ing"

interface BookingNotificationData {
  guestName: string
  guestEmail: string
  accommodationType: string
  amountPaid: string
  bookingSuccess: boolean
  venue?: string
  room?: string
  bedType?: string
  error?: string
}

export async function sendBookingNotification(
  data: BookingNotificationData
): Promise<boolean> {
  const transport = getTransporter()
  if (!transport) {
    console.log("[Email] SMTP not configured, skipping booking notification")
    return false
  }

  const statusColor = data.bookingSuccess ? "#16a34a" : "#dc2626"
  const statusLabel = data.bookingSuccess ? "ASSIGNED" : "FAILED"

  const flags: string[] = []
  if (!data.bookingSuccess) {
    flags.push(`Booking assignment failed: ${data.error || "unknown reason"}`)
  }
  if (!data.guestEmail) {
    flags.push("No email address on file for this guest")
  }

  const html = `
    <div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; color: #333;">
      <h2 style="margin-bottom: 4px;">Accommodation Update: ${data.guestName}</h2>
      <p style="margin-top: 0; color: ${statusColor}; font-weight: bold;">${statusLabel}</p>

      <table style="width: 100%; border-collapse: collapse; margin: 16px 0;">
        <tr><td style="padding: 4px 0;"><strong>Guest:</strong></td><td>${data.guestName}</td></tr>
        <tr><td style="padding: 4px 0;"><strong>Email:</strong></td><td>${data.guestEmail || "N/A"}</td></tr>
        <tr><td style="padding: 4px 0;"><strong>Paid:</strong></td><td>${data.amountPaid}</td></tr>
        <tr><td style="padding: 4px 0;"><strong>Requested:</strong></td><td>${data.accommodationType}</td></tr>
        ${data.bookingSuccess ? `
        <tr><td style="padding: 4px 0;"><strong>Assigned Venue:</strong></td><td>${data.venue}</td></tr>
        <tr><td style="padding: 4px 0;"><strong>Room:</strong></td><td>${data.room}</td></tr>
        <tr><td style="padding: 4px 0;"><strong>Bed Type:</strong></td><td>${data.bedType}</td></tr>
        ` : ""}
      </table>

      ${
        flags.length > 0
          ? `<div style="background: #fef2f2; padding: 12px 16px; border-radius: 6px; border-left: 3px solid #dc2626; margin: 16px 0;">
        <strong style="color: #dc2626;">Flags:</strong>
        <ul style="margin: 4px 0 0 0; padding-left: 20px;">${flags.map((f) => `<li>${f}</li>`).join("")}</ul>
      </div>`
          : `<p style="color: #16a34a;">No issues detected. <a href="https://docs.google.com/spreadsheets/d/1QJJNcxsWonmTBshvVVqr3ctNGHsoQbiR7mnjsxBuX9I/edit?gid=768527234#gid=768527234" style="color: #16a34a;">Booking sheet</a> updated automatically.</p>`
      }

      <p style="font-size: 12px; color: #666; margin-top: 24px;">Automated notification from CCG registration system</p>
    </div>
  `

  try {
    const info = await transport.sendMail({
      from: EMAIL_FROM,
      to: INTERNAL_NOTIFY_EMAIL,
      subject: `[CCG Booking] ${statusLabel}: ${data.guestName} — ${data.accommodationType}`,
      html,
    })
    console.log(`[Email] Booking notification sent to ${INTERNAL_NOTIFY_EMAIL} (${info.messageId})`)
    return true
  } catch (error) {
    console.error("[Email] Failed to send booking notification:", error)
    return false
  }
}

interface PaymentConfirmationData {
  name: string
  email: string
  amountPaid: string
  paymentMethod: string
  contributions: string
  dietary: string
  accommodationVenue?: string
  accommodationRoom?: string
}

export async function sendPaymentConfirmation(
  data: PaymentConfirmationData
): Promise<boolean> {
  const transport = getTransporter()
  if (!transport) {
    console.log("[Email] SMTP not configured, skipping confirmation email")
    return false
  }

  const html = `
    <div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; color: #333;">
      <h1 style="color: #f59e0b; margin-bottom: 8px;">You're In!</h1>
      <p style="font-size: 15px; color: #92400e; margin-top: 0; margin-bottom: 28px; font-style: italic;">Crypto Commons Gathering 2026</p>

      <p>Dear ${data.name},</p>

      <p>Your payment of <strong>${data.amountPaid}</strong> has been confirmed. You are now registered for <strong>Crypto Commons Gathering 2026</strong> in Austria's H&ouml;llental Valley, August 16&ndash;23, 2026.</p>

      <div style="background: #fffbeb; padding: 20px; border-radius: 8px; margin: 24px 0; border-left: 3px solid #f59e0b;">
        <h3 style="margin-top: 0; color: #92400e;">Registration Details</h3>
        <table style="width: 100%; border-collapse: collapse;">
          <tr>
            <td style="padding: 4px 0;"><strong>Amount:</strong></td>
            <td style="padding: 4px 0;">${data.amountPaid}</td>
          </tr>
          <tr>
            <td style="padding: 4px 0;"><strong>Payment:</strong></td>
            <td style="padding: 4px 0;">${data.paymentMethod}</td>
          </tr>
          ${data.dietary ? `<tr><td style="padding: 4px 0;"><strong>Dietary:</strong></td><td style="padding: 4px 0;">${data.dietary}</td></tr>` : ""}
          ${data.accommodationVenue ? `<tr><td style="padding: 4px 0;"><strong>Accommodation:</strong></td><td style="padding: 4px 0;">${data.accommodationVenue}${data.accommodationRoom ? `, Room ${data.accommodationRoom}` : ""}</td></tr>` : ""}
        </table>
      </div>

      <h3 style="color: #92400e;">Food &amp; Accommodation</h3>
      ${
        data.accommodationVenue
          ? `<p>Your accommodation at the <strong>${data.accommodationVenue}</strong>${data.accommodationRoom ? ` (Room ${data.accommodationRoom})` : ""} has been reserved for the duration of the gathering.</p>
      <p>We'll be in touch about food arrangements as we work to keep costs down while creating inclusive, participatory processes for the event.</p>`
          : `<p>We'll be in touch about food arrangements as we work to keep costs down while creating inclusive, participatory processes for the event. If you haven't yet decided on accommodation, the <strong>Commons Hub</strong> (our main venue) and the nearby <strong>Herrnhof Villa</strong> are the most convenient options &mdash; right in the heart of the gathering. Email <a href="mailto:contact@cryptocommonsgather.ing" style="color: #f59e0b;">contact@cryptocommonsgather.ing</a> for any questions.</p>`
      }

      <h3 style="color: #92400e;">What's Next?</h3>
      <ul style="line-height: 1.8;">
        <li>Join the <a href="https://t.me/+n5V_wDVKWrk1ZTBh" style="color: #f59e0b;">CCG26 Telegram group</a> to connect with other participants</li>
        <li>Start preparing your session proposals</li>
        <li>We'll follow up with you via email in the coming weeks with further details on logistics, schedule, and how to make the most of your time in the valley</li>
      </ul>

      <p style="margin-top: 32px;">
        See you in the valley,<br>
        <strong>The Crypto Commons Gathering Team</strong>
      </p>

      <hr style="border: none; border-top: 1px solid #ddd; margin: 32px 0;">
      <p style="font-size: 12px; color: #666;">
        You received this email because you registered at cryptocommonsgather.ing.<br>
        <a href="https://cryptocommonsgather.ing">cryptocommonsgather.ing</a>
      </p>
    </div>
  `

  try {
    const info = await transport.sendMail({
      from: EMAIL_FROM,
      to: data.email,
      bcc: INTERNAL_NOTIFY_EMAIL,
      subject: "Registration Confirmed - Crypto Commons Gathering 2026",
      html,
    })
    console.log(
      `[Email] Payment confirmation sent to ${data.email} (${info.messageId})`
    )
    return true
  } catch (error) {
    console.error("[Email] Failed to send payment confirmation:", error)
    return false
  }
}
