/**
 * Renders all reservation email templates with fixture data to
 * dist/email-previews/*.html for visual inspection.
 *
 * Usage: pnpm ts-node --swc --transpileOnly tools/email-preview/render.ts
 */
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import Handlebars from 'handlebars'
import mjml2html from 'mjml'

const TEMPLATE_DIR = join(__dirname, '../../apps/backend/src/assets/templates/mjml')
const OUT_DIR = join(__dirname, '../../dist/email-previews')

const base = {
	restaurant_name: 'Restaurant Beispielhaus',
	restaurant_logo_url: '',
	restaurant_address: 'Musterstraße 1, 12345 Musterstadt',
	restaurant_phone: '+49 555 0100000',
	restaurant_email: 'info@beispielhaus.example',
	google_place_id: '',
	date_long: 'Donnerstag, 20. August 2026',
	date_short: 'Do. 20.08.',
	time_start: '17:30',
	time_range: '17:30–19:30 Uhr',
	persons_label: '4 Personen',
	guest_name: 'Maxi Musterperson',
	guest_email: 'maxi@example.com',
	guest_phone: '+49 555 0123456',
	comment: 'Bitte Kinderstuhl bereitstellen.',
	manage_url: 'https://reservation-widget.kadicon.de/staging/manage/index.html?token=PREVIEW',
	maps_url: 'https://www.google.com/maps/search/?api=1&query=Google&query_place_id=PREVIEW',
	headline: 'Ihre Reservierungsanfrage ist eingegangen',
	is_pending: false,
	cancelled_by_guest: true,
	accept_url: 'https://example.com/accept',
	decline_url: 'https://example.com/decline',
	dashboard_url: 'https://example.com/dashboard',
	day_reservation_count: 12,
	day_guest_count: 38,
	changes: [
		{ label: 'Uhrzeit', oldValue: '17:30 Uhr', newValue: '18:00 Uhr' },
		{ label: 'Personen', oldValue: '4 Personen', newValue: '5 Personen' },
	],
}

const variants: Array<{ template: string; name: string; context: Record<string, unknown> }> = [
	{ template: 'reservation_guest_confirmed', name: 'guest_confirmed', context: base },
	{ template: 'reservation_guest_pending', name: 'guest_pending', context: base },
	{ template: 'reservation_guest_updated', name: 'guest_updated', context: base },
	{ template: 'reservation_guest_cancelled', name: 'guest_cancelled', context: base },
	{ template: 'reservation_owner_new', name: 'owner_new_confirmed', context: { ...base, headline: 'Neue Reservierung' } },
	{
		template: 'reservation_owner_new',
		name: 'owner_new_pending',
		context: { ...base, headline: 'Neue Reservierungsanfrage', is_pending: true },
	},
	{ template: 'reservation_owner_updated', name: 'owner_updated', context: base },
	{ template: 'reservation_owner_cancelled', name: 'owner_cancelled', context: base },
	{ template: 'reservation_reminder-minimal', name: 'guest_reminder', context: base },
]

async function main(): Promise<void> {
	mkdirSync(OUT_DIR, { recursive: true })

	let failures = 0
	for (const variant of variants) {
		const source = readFileSync(join(TEMPLATE_DIR, `${variant.template}.hbs`), 'utf8')
		const compiled = Handlebars.compile(source)(variant.context)
		// mjml v5 renders asynchronously (same call pattern as MjmlAdapter)
		const { html, errors = [] } = await mjml2html(compiled, { validationLevel: 'soft' })
		if (errors.length > 0) {
			failures++
			console.error(`✗ ${variant.name}:`, errors.map((error) => error.formattedMessage).join('; '))
		}
		writeFileSync(join(OUT_DIR, `${variant.name}.html`), html)
		console.log(`✓ ${variant.name}.html`)
	}

	if (failures > 0) {
		console.error(`${failures} template(s) had MJML errors`)
		process.exit(1)
	}
	console.log(`Alle Previews in ${OUT_DIR}`)
}

main().catch((error) => {
	console.error(error)
	process.exit(1)
})
