/**
 * Shared utilities for HTML template rendering.
 *
 * HTML templates use {{variable}} placeholders resolved against the
 * data context (client, matter, and extended annual-compliance data).
 *
 * resolveHtmlTemplate() is the main entry point: it takes an HTML string
 * and a flat key→value map and replaces all {{key}} occurrences.
 *
 * Fonts are embedded as base64 data URIs so rendering works entirely
 * offline — no Google Fonts CDN dependency, no network round-trip per render.
 */

import * as fs from 'fs'
import * as path from 'path'

export function resolveHtmlTemplate(html: string, data: Record<string, string>): string {
  return html.replace(/\{\{([^}]+)\}\}/g, (_, key) => {
    const trimmed = key.trim()
    return data[trimmed] ?? ''
  })
}

/** Load Sarabun font files once at module init and cache as base64 data URIs. */
function loadFontBase64(filename: string): string {
  const fontPath = path.join(__dirname, 'fonts', filename)
  try {
    return fs.readFileSync(fontPath).toString('base64')
  } catch {
    // Font file missing — fall back to system fonts gracefully
    console.warn(`[html-templates] Font not found: ${fontPath} — Thai rendering will use system fonts`)
    return ''
  }
}

const SARABUN_400 = loadFontBase64('sarabun-400.ttf')
const SARABUN_600 = loadFontBase64('sarabun-600.ttf')
const SARABUN_700 = loadFontBase64('sarabun-700.ttf')

function fontFace(weight: string, b64: string): string {
  if (!b64) return ''
  return `@font-face {
  font-family: 'Sarabun';
  font-style: normal;
  font-weight: ${weight};
  font-display: block;
  src: url('data:font/truetype;base64,${b64}') format('truetype');
}`
}

/** A4 print CSS with bundled Sarabun Thai font — no external requests. */
export const BASE_STYLE = `
${fontFace('400', SARABUN_400)}
${fontFace('600', SARABUN_600)}
${fontFace('700', SARABUN_700)}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Sarabun', 'TH Sarabun New', Arial, sans-serif;
  font-size: 13pt;
  line-height: 1.65;
  color: #000;
  background: #fff;
}

@page {
  size: A4;
  margin: 22mm 20mm 22mm 20mm;
}

@media print {
  body { margin: 0; }
  .no-print { display: none !important; }
  .page-break { page-break-before: always; }
}

h1 {
  font-size: 14pt;
  font-weight: 700;
  text-align: center;
  margin-bottom: 3px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

h2 {
  font-size: 13pt;
  font-weight: 700;
  text-align: center;
  margin-bottom: 3px;
}

.company-block {
  text-align: center;
  margin-bottom: 18px;
}

.divider {
  border: none;
  border-top: 1.5px solid #000;
  margin: 20px 0;
}

.thin-divider {
  border: none;
  border-top: 0.5px solid #888;
  margin: 14px 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 12pt;
}

th, td {
  border: 1px solid #333;
  padding: 5px 8px;
  text-align: left;
  vertical-align: top;
}

th {
  background: #f5f5f5;
  font-weight: 600;
}

td.right { text-align: right; }
td.center { text-align: center; }

.agenda-item {
  margin: 8px 0 8px 20px;
}

.signature-block {
  display: flex;
  justify-content: space-around;
  margin-top: 50px;
  gap: 20px;
}

.sig-slot {
  text-align: center;
  min-width: 200px;
}

.sig-line {
  border-bottom: 1px solid #000;
  height: 45px;
  width: 100%;
  margin-bottom: 6px;
}

.seal-box {
  border: 1.5px dashed #aaa;
  width: 80px;
  height: 80px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8pt;
  color: #aaa;
  border-radius: 50%;
}

.date-right {
  text-align: right;
  margin-bottom: 16px;
}

p { margin: 8px 0; }

strong { font-weight: 700; }

.indent { margin-left: 28px; }

.label-row {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
}

.label-row .label {
  font-weight: 600;
  min-width: 160px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
}

.checkbox {
  width: 14px;
  height: 14px;
  border: 1px solid #333;
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
}

.bilingual-block {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
}
`
