#!/bin/bash
# Write privacy policy files (txt/md/html) into a TERMS/PRIVACY dir.
# Used by exchange + merchant installers. Style matches dual-currency terms.
#
# Usage (sourced or): install_privacy_docs DIR ETAG TITLE BADGE
# Environment: BODY_MD, BODY_TXT, BODY_HTML_MAIN (inner body HTML after h1)
set -euo pipefail

install_privacy_docs() {
  local dir="$1" etag="$2" title="$3" badge="$4"
  local lang="${5:-en}"
  local out="$dir/$lang"
  mkdir -p "$out"

  printf '%s\n' "${BODY_TXT:?}" >"$out/${etag}.txt"
  printf '%s\n' "${BODY_MD:?}" >"$out/${etag}.md"

  cat >"$out/${etag}.html" <<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>${title}</title>
  <style>
    :root { color-scheme: dark light; }
    body {
      font-family: system-ui, -apple-system, sans-serif;
      max-width: 42rem; margin: 2rem auto; padding: 0 1.1rem 3rem;
      line-height: 1.5; color: #e8e6e3; background: #1a1520;
    }
    h1 { font-size: 1.35rem; font-weight: 800; margin: 0 0 1rem; color: #f5f0ea; }
    h2 { font-size: 1.05rem; margin: 1.5rem 0 0.5rem; color: #e8c878; }
    h3 { font-size: 0.95rem; margin: 1rem 0 0.4rem; color: #c4b5fd; }
    p, li, td, th { font-size: 0.95rem; }
    ul, ol { padding-left: 1.2rem; }
    code, a { color: #5eead4; }
    a { text-decoration: none; }
    a:hover { text-decoration: underline; }
    .badge {
      display: inline-block; font-size: 0.72rem; font-weight: 700;
      letter-spacing: 0.06em; text-transform: uppercase;
      color: #c4b5fd; border: 1px solid rgba(196,181,253,0.35);
      border-radius: 999px; padding: 0.2rem 0.65rem; margin-bottom: 0.85rem;
    }
    .note {
      border-radius: 12px; padding: 0.75rem 0.9rem; margin: 0.85rem 0 1rem;
      border: 1px solid rgba(255,255,255,0.1); background: rgba(0,0,0,0.25);
      font-size: 0.9rem; color: #c8c4bf;
    }
    table {
      width: 100%; border-collapse: collapse; margin: 0.6rem 0 1rem;
      font-size: 0.88rem;
    }
    th, td {
      border: 1px solid rgba(255,255,255,0.12); padding: 0.45rem 0.55rem;
      text-align: left; vertical-align: top;
    }
    th { background: rgba(0,0,0,0.35); color: #e8c878; font-weight: 700; }
    .muted { color: #a39e98; font-size: 0.88rem; }
    footer { margin-top: 2rem; font-size: 0.85rem; color: #a39e98; }
  </style>
</head>
<body>
  <div class="badge">${badge}</div>
  <h1>${title}</h1>
${BODY_HTML_MAIN}
  <footer class="muted">Version ${etag} · Swiss FADP (revDSG) · hacktivism.ch</footer>
</body>
</html>
HTML
  chmod -R a+rX "$dir"
  echo "privacy docs: $out/${etag}.{txt,md,html}"
}
