#!/usr/bin/env bash
set -euo pipefail

OUT_DIR="${OUT_DIR:-public-showcase}"
THEMES="${THEMES:-default,teletype-clean,teletype-balanced,teletype-worn}"
PREVIEW_PATH="${PREVIEW_PATH:-/}"
CONFIG="${CONFIG:-config.yaml}"
CONTENT_DIR="${CONTENT_DIR:-content}"
STATIC_DIR="${STATIC_DIR:-static}"
THEMES_DIR="${THEMES_DIR:-themes}"
INCLUDE_DRAFTS="${INCLUDE_DRAFTS:-false}"
INCLUDE_FUTURE="${INCLUDE_FUTURE:-false}"

trim() {
  local s="$1"
  s="${s#"${s%%[![:space:]]*}"}"
  s="${s%"${s##*[![:space:]]}"}"
  printf '%s' "$s"
}

normalize_preview_path() {
  local p
  p="$(trim "$1")"
  if [[ -z "$p" ]]; then
    p="/"
  fi
  if [[ "${p:0:1}" != "/" ]]; then
    p="/$p"
  fi
  if [[ "$p" == "/" ]]; then
    printf '%s' "$p"
    return
  fi

  local base="${p##*/}"
  if [[ "$base" != *.* && "$p" != */ ]]; then
    p="$p/"
  fi
  printf '%s' "$p"
}

PREVIEW_PATH="$(normalize_preview_path "$PREVIEW_PATH")"

declare -a THEME_LIST=()
declare -A SEEN=()
IFS=',' read -r -a RAW_THEMES <<<"$THEMES"
for raw in "${RAW_THEMES[@]}"; do
  t="$(trim "$raw")"
  if [[ -z "$t" ]]; then
    continue
  fi
  if [[ -n "${SEEN[$t]:-}" ]]; then
    continue
  fi
  SEEN["$t"]=1
  THEME_LIST+=("$t")
done

if [[ ${#THEME_LIST[@]} -eq 0 ]]; then
  echo "theme-showcase: no themes specified" >&2
  exit 1
fi

mkdir -p "$OUT_DIR/themes"

for theme in "${THEME_LIST[@]}"; do
  theme_out="$OUT_DIR/themes/$theme"
  cmd=(
    go run ./cmd/ssg build
    --config "$CONFIG"
    --content "$CONTENT_DIR"
    --static "$STATIC_DIR"
    --themes "$THEMES_DIR"
    --theme "$theme"
    --out "$theme_out"
  )
  if [[ "$INCLUDE_DRAFTS" == "true" ]]; then
    cmd+=(--include-drafts)
  fi
  if [[ "$INCLUDE_FUTURE" == "true" ]]; then
    cmd+=(--include-future)
  fi

  echo "Building theme: $theme"
  "${cmd[@]}"
done

sampler_file="$OUT_DIR/index.html"
{
  cat <<'HTML_HEAD'
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Theme Showcase</title>
  <style>
    :root {
      --bg: #0f1115;
      --panel: #171a20;
      --fg: #e5e7eb;
      --muted: #9ca3af;
      --border: #2a2f3a;
      --accent: #88d8ff;
    }
    * { box-sizing: border-box; }
    body {
      margin: 0;
      font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
      background: radial-gradient(circle at 15% 0%, #1d2433, var(--bg) 55%);
      color: var(--fg);
      padding: 1rem;
    }
    h1 { margin: 0 0 0.35rem; font-size: 1.15rem; }
    p { margin: 0 0 1rem; color: var(--muted); font-size: 0.9rem; }
    .grid {
      display: grid;
      gap: 0.9rem;
      grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    }
    .card {
      border: 1px solid var(--border);
      border-radius: 10px;
      background: var(--panel);
      overflow: hidden;
    }
    .card-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid var(--border);
      padding: 0.55rem 0.75rem;
      gap: 0.8rem;
    }
    .theme-name { font-size: 0.9rem; }
    a {
      color: var(--accent);
      text-decoration: none;
      font-size: 0.82rem;
    }
    a:hover, a:focus-visible {
      text-decoration: underline;
      outline: none;
    }
    iframe {
      width: 100%;
      height: 520px;
      border: 0;
      background: white;
      display: block;
    }
  </style>
</head>
<body>
  <h1>Theme Showcase</h1>
  <p>Live previews generated by <code>scripts/theme-showcase.sh</code></p>
  <div class="grid">
HTML_HEAD

  for theme in "${THEME_LIST[@]}"; do
    theme_href="/themes/$theme/"
    iframe_src="/themes/$theme$PREVIEW_PATH"
    cat <<HTML_CARD
    <section class="card">
      <header class="card-header">
        <div class="theme-name">$theme</div>
        <a href="$theme_href">Open Theme</a>
      </header>
      <iframe src="$iframe_src" loading="lazy" title="Theme preview: $theme"></iframe>
    </section>
HTML_CARD
  done

  cat <<'HTML_FOOT'
  </div>
</body>
</html>
HTML_FOOT
} >"$sampler_file"

echo "Theme showcase ready: $sampler_file"
