// Renders every email template with sample data into a folder, as HTML, so
// the frame can be looked at in a browser (and screenshotted) without sending
// anything.   npx tsx scripts/email-preview.ts <outDir>
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import * as t from '../src/lib/email-templates';

const out = process.argv[2] || 'email-previews';
mkdirSync(out, { recursive: true });
const brand = { firm: 'Sunrise Hostels Pvt Ltd', accent: '#2563eb', address: '12 MG Road, Vidisha, MP 464001', phone: '+91 98260 12345', email: 'office@sunrisehostels.in' };
const invoice: t.InvoiceFacts = { number: 'INV-2026-10-0042', kind: 'Rent', periodLabel: 'October 2026', amount: 7500, paid: 7500, balance: 0, dueDate: '05 Oct 2026' };
const partly: t.InvoiceFacts = { ...invoice, paid: 3000, balance: 4500 };
const samples: Record<string, t.EmailMessage> = {
  'otp-login': t.otpEmail({ name: 'Aarav', code: '482913', minutes: 10, purpose: 'LOGIN' }),
  'payment-received': t.paymentReceivedEmail({ firm: brand.firm, brand, studentName: 'Aarav Mehta', amount: 7500, paidOn: '04 Oct 2026', method: 'UPI', reference: 'UTR 428811234567', paymentId: 'pay_R4k2x9', invoice, receiptAttached: true }),
  'payment-failed': t.paymentFailedEmail({ firm: brand.firm, brand, studentName: 'Aarav Mehta', amount: 4500, paidOn: '03 Oct 2026', method: 'Cheque', reference: 'CHQ 001234', paymentId: 'pay_R4k2x7', reason: 'Cheque returned unpaid', invoice: partly, contact: '+91 98260 12345' }),
  'invoice-raised': t.invoiceRaisedEmail({ firm: brand.firm, brand, studentName: 'Priya Singh', invoice: { ...invoice, paid: 0, balance: 7500 }, cycleMonths: 1 }),
  'payment-link': t.paymentLinkEmail({ firm: brand.firm, brand, studentName: 'Priya Singh', invoiceNumber: invoice.number, kind: 'Rent', periodLabel: 'October 2026', baseAmount: 7500, grossAmount: 7677, feeBearer: 'STUDENT', dueDate: '05 Oct 2026', linkUrl: 'https://rzp.io/l/sunrise-oct', expiresAt: new Date('2026-10-05T14:30:00+05:30') }),
  'fee-reminder': t.feeReminderEmail({ firm: brand.firm, brand, studentName: 'Rohan Verma', invoice: { ...invoice, paid: 0, balance: 7500 }, when: 'in 3 days', overdue: false, payUrl: 'https://rzp.io/l/sunrise-oct', payAmount: 7677 }),
  'fee-overdue': t.feeReminderEmail({ firm: brand.firm, brand, studentName: 'Rohan Verma', invoice: { ...invoice, paid: 0, balance: 7500 }, when: '5 days ago', overdue: true }),
  'settlement-refund': t.settlementSharedEmail({ firm: brand.firm, brand, studentName: 'Kabir Shah', lastDay: '30 Nov 2026', depositHeld: 15000, dues: 0, deductions: [{ label: 'Broken study chair', amount: 1200 }, { label: 'Lost room key', amount: 300 }], refundDue: 13500, shortfall: 0, promisedDate: '15 Dec 2026', attached: true }),
  'settlement-shortfall': t.settlementSharedEmail({ firm: brand.firm, brand, studentName: 'Kabir Shah', lastDay: '30 Nov 2026', depositHeld: 5000, dues: 4500, deductions: [{ label: 'Mattress replacement', amount: 2500 }], refundDue: 0, shortfall: 2000, promisedDate: '15 Dec 2026', attached: false }),
  'late-return-guardian': t.lateReturnEmail({ firm: brand.firm, brand, recipientName: 'Suresh Singh', studentName: 'Priya Singh', dueBack: '04 Oct, 7:30 pm', destination: 'Bhopal', forGuardian: true }),
  'late-return-staff': t.lateReturnEmail({ firm: brand.firm, brand, recipientName: 'Mohan Lal', studentName: 'Priya Singh', dueBack: '04 Oct, 7:30 pm', destination: 'Bhopal', forGuardian: false }),
  'enquiry': t.enquiryEmail({ firm: 'Vidisha', brand, name: 'Neha Prospect', phone: '+91 98111 22233', email: 'neha@example.com', roomType: 'Double', moveIn: '01 Nov 2026', message: 'Is a double free from next month? Two of us from the same college.' }),
  'guardian-invite': t.guardianInviteEmail({ firm: brand.firm, brand, guardianName: 'Suresh Singh', studentName: 'Priya Singh', email: 'suresh@example.com', temporaryPassword: 'G-7KQ2-M9XA' }),
  'notice': t.noticeEmail({ firm: brand.firm, brand, recipientName: 'Aarav', title: 'Water supply off on Sunday morning', body: 'The overhead tank is being cleaned on Sunday 11 October from 6 am to 11 am.\n\nPlease fill what you need on Saturday night. The mess will run as usual.', postedBy: 'Sia Kapoor', category: 'GENERAL' }),
  'no-brand-defaults': t.invoiceRaisedEmail({ firm: 'HostelCare', studentName: 'Priya Singh', invoice: { ...invoice, paid: 0, balance: 7500 }, cycleMonths: 3 }),
};
const index: string[] = [];
for (const [name, m] of Object.entries(samples)) {
  writeFileSync(join(out, `${name}.html`), m.html);
  writeFileSync(join(out, `${name}.txt`), `Subject: ${m.subject}\n\n${m.text}\n`);
  index.push(`<li><a href="${name}.html">${name}</a> — ${m.subject}</li>`);
}
writeFileSync(join(out, 'index.html'), `<!doctype html><meta charset="utf-8"><title>HostelCare email previews</title><body style="font-family:sans-serif;padding:24px"><h1>HostelCare email previews</h1><ul>${index.join('')}</ul>`);
console.log(`${Object.keys(samples).length} previews written to ${out}`);
