import { Resend } from "resend";
import { SITE } from "@/lib/site";

let client: Resend | null = null;

export function getClient() {
  if (!client) client = new Resend(process.env.RESEND_API_KEY);
  return client;
}

const logoHtml = `<img src="${SITE.siteUrl}/brand/logo.png" alt="${SITE.name}" width="48" height="48" style="display:block;margin-bottom:12px;border-radius:50%;" />`;

export async function sendNewOrderNotification(to: string, order: { orderNumber: string; customerName: string; total: number }) {
  const from = process.env.EMAIL_FROM || "Iniyaval Return Gifts <onboarding@resend.dev>";
  const amount = new Intl.NumberFormat("en-IN", { style: "currency", currency: "INR", maximumFractionDigits: 0 }).format(order.total / 100);

  await getClient().emails.send({
    from,
    to,
    subject: `New order ${order.orderNumber} — ${amount}`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 420px; margin: 0 auto; padding: 24px;">
        ${logoHtml}
        <h2 style="color: #9f1239;">New paid order</h2>
        <p><strong>${order.orderNumber}</strong> from ${order.customerName}</p>
        <p style="font-size: 24px; font-weight: bold; color: #1c1917;">${amount}</p>
        <p style="color: #78716c; font-size: 13px;">Open the admin panel to see the full order and start fulfilling it.</p>
      </div>
    `,
  });
}

export async function sendNewTicketNotification(to: string, ticket: { id: string; name: string; subject: string }) {
  const from = process.env.EMAIL_FROM || "Iniyaval Return Gifts <onboarding@resend.dev>";

  await getClient().emails.send({
    from,
    to,
    subject: `New support ticket: ${ticket.subject}`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 420px; margin: 0 auto; padding: 24px;">
        ${logoHtml}
        <h2 style="color: #9f1239;">New support ticket</h2>
        <p><strong>${ticket.subject}</strong> from ${ticket.name}</p>
        <p style="color: #78716c; font-size: 13px;">Open the admin panel to read and reply.</p>
      </div>
    `,
  });
}

export async function sendTicketReplyEmail(to: string, ticket: { subject: string; replyBody: string }) {
  const from = process.env.EMAIL_FROM || "Iniyaval Return Gifts <onboarding@resend.dev>";

  await getClient().emails.send({
    from,
    to,
    subject: `Re: ${ticket.subject}`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 420px; margin: 0 auto; padding: 24px; white-space: pre-line;">
        ${logoHtml}
        <h2 style="color: #9f1239;">${SITE.name} support</h2>
        <p>${ticket.replyBody}</p>
        <p style="color: #78716c; font-size: 13px;">Reply on the site to continue this conversation.</p>
      </div>
    `,
  });
}

export async function sendPasswordResetEmail(email: string, resetUrl: string) {
  const from = process.env.EMAIL_FROM || "Iniyaval Return Gifts <onboarding@resend.dev>";

  await getClient().emails.send({
    from,
    to: email,
    subject: `Reset your ${SITE.name} password`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 420px; margin: 0 auto; padding: 24px;">
        ${logoHtml}
        <h2 style="color: #9f1239;">${SITE.name}</h2>
        <p>We got a request to reset your password. Click below to choose a new one:</p>
        <p style="margin: 20px 0;">
          <a href="${resetUrl}" style="display: inline-block; background: #9f1239; color: #fff; padding: 10px 20px; border-radius: 6px; text-decoration: none; font-weight: bold;">Reset password</a>
        </p>
        <p style="color: #78716c; font-size: 13px;">This link expires in 30 minutes. If you didn't request this, you can ignore this email — your password won't change.</p>
      </div>
    `,
  });
}

export async function sendOtpEmail(email: string, code: string) {
  const from = process.env.EMAIL_FROM || "Iniyaval Return Gifts <onboarding@resend.dev>";

  await getClient().emails.send({
    from,
    to: email,
    subject: `${code} is your ${SITE.name} sign-in code`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 420px; margin: 0 auto; padding: 24px;">
        ${logoHtml}
        <h2 style="color: #9f1239;">${SITE.name}</h2>
        <p>Your sign-in code is:</p>
        <p style="font-size: 32px; font-weight: bold; letter-spacing: 6px; color: #1c1917;">${code}</p>
        <p style="color: #78716c; font-size: 13px;">This code expires in 10 minutes. If you didn't request this, you can ignore this email.</p>
      </div>
    `,
  });
}
