#!/usr/bin/env bash
# Showcase script for the zalgoctl CLI
set -euo pipefail

ZALGOCTL="$(dirname "$0")/zalgoctl"
SEED=42  # reproducible demos

# ── helpers ──────────────────────────────────────────────────────────────────

BOLD='\033[1m'
DIM='\033[2m'
CYAN='\033[36m'
GREEN='\033[32m'
YELLOW='\033[33m'
MAGENTA='\033[35m'
RESET='\033[0m'

section() {
    printf '\n%b━━━ %s ━━━%b\n\n' "$MAGENTA$BOLD" "$1" "$RESET"
}

demo() {
    local desc="$1"; shift
    printf '  %b# %s%b\n' "$DIM" "$desc" "$RESET"
    printf '  %b$ %s%b\n' "$CYAN" "$*" "$RESET"
    printf '  '
    eval "$@" 2>&1
    printf '\n\n'
}

# ── intro ────────────────────────────────────────────────────────────────────

printf '%b' "$BOLD$MAGENTA"
$ZALGOCTL --style subtle --seed $SEED << 'BANNER'
 ______     _
|__  / |__ | | __ _  ___
  / /| '_ \| |/ _` |/ _ \
 / /_| | | | | (_| | (_) |
/____|_| |_|_|\__, |\___/
              |___/
BANNER
printf '%b' "$RESET"
echo "Showcase - every feature demonstrated" | $ZALGOCTL --style whisper --seed $SEED
printf '\n'

# ── basics ───────────────────────────────────────────────────────────────────

section "BASICS"

demo "Default encode (moderate intensity)" \
    "$ZALGOCTL" encode '"Hello World"' --seed "$SEED"

demo "Pipe text through stdin" \
    "echo 'He comes' | $ZALGOCTL --seed $SEED"

demo "Encode is the default subcommand" \
    "$ZALGOCTL" '"The abyss stares back"' --seed "$SEED"

# ── style presets ────────────────────────────────────────────────────────────

section "STYLE PRESETS"

demo "whisper — barely there" \
    "$ZALGOCTL" encode '"Whisper in the dark"' --style whisper --seed "$SEED"

demo "subtle — readable but eerie" \
    "$ZALGOCTL" encode '"Something is wrong"' --style subtle --seed "$SEED"

demo "moderate — classic Zalgo (default)" \
    "$ZALGOCTL" encode '"He waits behind the wall"' --style moderate --seed "$SEED"

demo "heavy — serious corruption" \
    "$ZALGOCTL" encode '"The door is opening"' --style heavy --seed "$SEED"

demo "extreme — full chaos" \
    "$ZALGOCTL" encode '"Fear me"' --style extreme --seed "$SEED"

demo "demonic — screen-destroying" \
    "$ZALGOCTL" encode '"Obey"' --style demonic --seed "$SEED"

demo "void — the abyss stares back" \
    "$ZALGOCTL" encode '"End"' --style void --seed "$SEED"

# ── directional presets ──────────────────────────────────────────────────────

section "DIRECTIONAL PRESETS"

demo "up-only — reaching skyward" \
    "$ZALGOCTL" encode '"Rising"' --style up-only --seed "$SEED"

demo "down-only — melting downward" \
    "$ZALGOCTL" encode '"Sinking"' --style down-only --seed "$SEED"

demo "mid-only — in-place corruption" \
    "$ZALGOCTL" encode '"Corrupted"' --style mid-only --seed "$SEED"

demo "ascend — heavy up, light down" \
    "$ZALGOCTL" encode '"Ascending"' --style ascend --seed "$SEED"

demo "descend — light up, heavy down" \
    "$ZALGOCTL" encode '"Descending"' --style descend --seed "$SEED"

# ── manual intensity ─────────────────────────────────────────────────────────

section "MANUAL INTENSITY CONTROL"

demo "Set all directions with --intensity" \
    "$ZALGOCTL" encode '"Uniform chaos"' --intensity 15 --seed "$SEED"

demo "Only above marks" \
    "$ZALGOCTL" encode '"Sky high"' --up 20 --mid 0 --down 0 --seed "$SEED"

demo "Only below marks" \
    "$ZALGOCTL" encode '"Underground"' --up 0 --mid 0 --down 20 --seed "$SEED"

demo "Only middle overlays" \
    "$ZALGOCTL" encode '"Overwritten"' --up 0 --mid 15 --down 0 --seed "$SEED"

demo "Override a preset (heavy + no middle)" \
    "$ZALGOCTL" encode '"Heavy but clean"' --style heavy --mid 0 --seed "$SEED"

demo "Minimum intensity" \
    "$ZALGOCTL" encode '"Barely touched"' --up 1 --mid 0 --down 1 --variance 0 --seed "$SEED"

demo "Maximum intensity" \
    "$ZALGOCTL" encode '"Max"' --up 100 --mid 10 --down 100 --seed "$SEED"

# ── variance ─────────────────────────────────────────────────────────────────

section "VARIANCE CONTROL"

demo "Zero variance — perfectly uniform marks per char" \
    "$ZALGOCTL" encode '"Uniform uniform uniform"' --intensity 8 --variance 0 --seed "$SEED"

demo "Full variance — wildly different per char" \
    "$ZALGOCTL" encode '"Chaos chaos chaos"' --intensity 8 --variance 1.0 --seed "$SEED"

# ── patterns ─────────────────────────────────────────────────────────────────

section "PATTERN MODES"

demo "wave — sinusoidal intensity" \
    "$ZALGOCTL" encode '"The wave rolls through the darkness"' --pattern wave --intensity 20 --seed "$SEED"

demo "fade-in — builds from nothing" \
    "$ZALGOCTL" encode '"Growing horror approaches"' --pattern fade-in --intensity 25 --seed "$SEED"

demo "fade-out — dissolves to nothing" \
    "$ZALGOCTL" encode '"Fading into silence"' --pattern fade-out --intensity 25 --seed "$SEED"

demo "pulse — square wave on/off" \
    "$ZALGOCTL" encode '"On and off and on and off"' --pattern pulse --intensity 15 --seed "$SEED"

demo "crescendo — exponential ramp up" \
    "$ZALGOCTL" encode '"Slowly then all at once"' --pattern crescendo --intensity 30 --seed "$SEED"

demo "decrescendo — exponential ramp down" \
    "$ZALGOCTL" encode '"Overwhelming then calm"' --pattern decrescendo --intensity 30 --seed "$SEED"

demo "spike — peak in the center" \
    "$ZALGOCTL" encode '"The center cannot hold"' --pattern spike --intensity 25 --seed "$SEED"

demo "sawtooth — repeating ramp" \
    "$ZALGOCTL" encode '"Again and again and again"' --pattern sawtooth --intensity 20 --seed "$SEED"

demo "heartbeat — double-beat cardiac rhythm" \
    "$ZALGOCTL" encode '"Can you hear it beating"' --pattern heartbeat --intensity 25 --seed "$SEED"

# ── wave tuning ──────────────────────────────────────────────────────────────

section "WAVE FREQUENCY & PHASE"

demo "wave with 2x frequency — faster oscillation" \
    "$ZALGOCTL" encode '"Faster faster faster faster"' --pattern wave --wave-freq 2 --intensity 20 --seed "$SEED"

demo "wave with 3x frequency" \
    "$ZALGOCTL" encode '"Rapid oscillation across text"' --pattern wave --wave-freq 3 --intensity 20 --seed "$SEED"

demo "wave with phase offset (pi/2)" \
    "$ZALGOCTL" encode '"Shifted starting point"' --pattern wave --wave-phase 1.5708 --intensity 20 --seed "$SEED"

demo "sawtooth with 3x frequency" \
    "$ZALGOCTL" encode '"Ramp ramp ramp ramp ramp"' --pattern sawtooth --wave-freq 3 --intensity 15 --seed "$SEED"

# ── unicode blocks ───────────────────────────────────────────────────────────

section "UNICODE BLOCKS"

demo "core only (default) — standard combining marks" \
    "$ZALGOCTL" encode '"Standard marks"' --blocks core --intensity 10 --seed "$SEED"

demo "extended block — rare diacriticals" \
    "$ZALGOCTL" encode '"Extended marks"' --blocks extended --intensity 10 --seed "$SEED"

demo "supplement block — additional diacriticals" \
    "$ZALGOCTL" encode '"Supplement marks"' --blocks supplement --intensity 10 --seed "$SEED"

demo "symbols block — marks for symbols" \
    "$ZALGOCTL" encode '"Symbol marks"' --blocks symbols --intensity 10 --seed "$SEED"

demo "all blocks combined — maximum variety" \
    "$ZALGOCTL" encode '"Everything at once"' --blocks all --intensity 10 --seed "$SEED"

demo "mix specific blocks" \
    "$ZALGOCTL" encode '"Core plus symbols"' --blocks core --blocks symbols --intensity 10 --seed "$SEED"

# ── mark pools ──────────────────────────────────────────────────────────────

section "MARK POOLS"

demo "classic — matches traditional Zalgo generators (dense, chaotic)" \
    "$ZALGOCTL" encode '"The old ways"' --pool classic --intensity 10 --seed "$SEED"

demo "curated — selective core marks (clean, reliable)" \
    "$ZALGOCTL" encode '"Refined corruption"' --pool curated --intensity 10 --seed "$SEED"

demo "dense — heavy stacking marks (thick, crossed-out)" \
    "$ZALGOCTL" encode '"Obliterated"' --pool dense --intensity 10 --seed "$SEED"

demo "geometric — lines, arrows, angular marks (glitch aesthetic)" \
    "$ZALGOCTL" encode '"System error"' --pool geometric --intensity 10 --seed "$SEED"

demo "full — all Unicode combining blocks (maximum variety)" \
    "$ZALGOCTL" encode '"Everything everywhere"' --pool full --intensity 10 --seed "$SEED"

demo "pool + style combo — classic pool with heavy style" \
    "$ZALGOCTL" encode '"Old school chaos"' --pool classic --style heavy --seed "$SEED"

demo "pool + pattern combo — geometric pool with pulse" \
    "$ZALGOCTL" encode '"Glitch pulse glitch pulse"' --pool geometric --pattern pulse --intensity 15 --seed "$SEED"

# ── mark filters ─────────────────────────────────────────────────────────────

section "MARK FILTERS"

demo "accents — grave, acute, circumflex, etc." \
    "$ZALGOCTL" encode '"Accented text"' --marks accents --intensity 8 --seed "$SEED"

demo "dots — dots, rings, diaereses" \
    "$ZALGOCTL" encode '"Dotted text"' --marks dots --intensity 8 --seed "$SEED"

demo "lines — strokes, overlines, underlines" \
    "$ZALGOCTL" encode '"Lined text"' --marks lines --intensity 8 --seed "$SEED"

demo "letters — medieval superscript letters" \
    "$ZALGOCTL" encode '"Ancient text"' --marks letters --up 5 --mid 0 --down 0 --seed "$SEED"

demo "arrows — arrow marks" \
    "$ZALGOCTL" encode '"Directed"' --marks arrows --up 6 --mid 0 --down 0 --seed "$SEED"

demo "hooks — hooks, horns, cedillas" \
    "$ZALGOCTL" encode '"Hooked text"' --marks hooks --intensity 8 --seed "$SEED"

# ── character filtering ──────────────────────────────────────────────────────

section "CHARACTER FILTERING"

demo "Default: spaces are skipped" \
    "$ZALGOCTL" encode '"Spaces are clean"' --intensity 10 --seed "$SEED"

demo "No skip spaces — corrupt everything" \
    "$ZALGOCTL" encode '"Spaces are dirty"' --no-skip-spaces --intensity 10 --seed "$SEED"

demo "Skip punctuation" \
    "$ZALGOCTL" encode '"Hello, World! How are you?"' --skip-punct --intensity 10 --seed "$SEED"

demo "Skip numbers" \
    "$ZALGOCTL" encode '"Agent 007 reporting at 0900"' --skip-numbers --intensity 10 --seed "$SEED"

demo "Only ASCII letters" \
    "$ZALGOCTL" encode '"cafe 123 ok!"' --only-ascii --intensity 10 --seed "$SEED"

demo "All filters combined" \
    "$ZALGOCTL" encode '"Hello, World! 42 is the answer."' --skip-punct --skip-numbers --only-ascii --intensity 10 --seed "$SEED"

# ── probability (sparse corruption) ─────────────────────────────────────────

section "PROBABILITY (SPARSE CORRUPTION)"

demo "100% probability (default) — every character affected" \
    "$ZALGOCTL" encode '"All characters corrupted"' --prob 1.0 --intensity 10 --seed "$SEED"

demo "50% probability — half the characters affected" \
    "$ZALGOCTL" encode '"Half the characters corrupted"' --prob 0.5 --intensity 10 --seed "$SEED"

demo "25% probability — sparse corruption" \
    "$ZALGOCTL" encode '"Sparse scattered corruption through text"' --prob 0.25 --intensity 10 --seed "$SEED"

demo "10% probability — occasional glitches" \
    "$ZALGOCTL" encode '"Mostly clean with occasional glitches here and there"' --prob 0.1 --intensity 15 --seed "$SEED"

demo "35% probability — the classic Python recipe" \
    "$ZALGOCTL" encode '"He comes"' --pool classic --prob 0.35 --intensity 10 --seed "$SEED"

demo "Sparse + high intensity — rare but intense" \
    "$ZALGOCTL" encode '"When it hits it hits hard"' --prob 0.2 --intensity 40 --seed "$SEED"

demo "Sparse + pattern — fade-in with 30% probability" \
    "$ZALGOCTL" encode '"Building dread through scattered marks"' --prob 0.3 --pattern fade-in --intensity 20 --seed "$SEED"

# ── max marks cap ────────────────────────────────────────────────────────────

section "MAX MARKS CAP"

demo "High intensity, capped to 3 marks per char" \
    "$ZALGOCTL" encode '"Controlled chaos"' --intensity 50 --max-marks 3 --seed "$SEED"

demo "High intensity, capped to 5 marks per char" \
    "$ZALGOCTL" encode '"Controlled chaos"' --intensity 50 --max-marks 5 --seed "$SEED"

demo "High intensity, capped to 10 marks per char" \
    "$ZALGOCTL" encode '"Controlled chaos"' --intensity 50 --max-marks 10 --seed "$SEED"

# ── reproducibility ──────────────────────────────────────────────────────────

section "REPRODUCIBILITY"

demo "Seed 42 — run 1" \
    "$ZALGOCTL" encode '"Deterministic output"' --intensity 10 --seed 42

demo "Seed 42 — run 2 (identical)" \
    "$ZALGOCTL" encode '"Deterministic output"' --intensity 10 --seed 42

demo "Seed 99 — different seed, different result" \
    "$ZALGOCTL" encode '"Deterministic output"' --intensity 10 --seed 99

# ── decode ───────────────────────────────────────────────────────────────────

section "DECODE"

demo "Full decode — strip all combining marks" \
    "echo 'Round trip:' && $ZALGOCTL encode 'Hello World' --intensity 20 --seed $SEED | $ZALGOCTL decode"

demo "Partial decode — only strip chars with >10 marks" \
    "$ZALGOCTL" encode '"Threshold test"' --intensity 20 --seed "$SEED" '|' "$ZALGOCTL" decode --threshold 10

demo "Partial decode — only strip chars with >5 marks" \
    "$ZALGOCTL" encode '"Threshold test"' --intensity 20 --seed "$SEED" '|' "$ZALGOCTL" decode --threshold 5

# ── analyze ──────────────────────────────────────────────────────────────────

section "ANALYZE"

demo "Analyze whisper-level text" \
    "$ZALGOCTL" encode '"Analyze me"' --style whisper --seed "$SEED" '|' "$ZALGOCTL" analyze

demo "Analyze heavy-level text" \
    "$ZALGOCTL" encode '"Analyze me"' --style heavy --seed "$SEED" '|' "$ZALGOCTL" analyze

demo "Analyze extreme-level text" \
    "$ZALGOCTL" encode '"Analyze me"' --style extreme --seed "$SEED" '|' "$ZALGOCTL" analyze

demo "Analyze clean text (no Zalgo)" \
    "echo 'Plain text' | $ZALGOCTL analyze"

# ── creative combos ──────────────────────────────────────────────────────────

section "CREATIVE COMBINATIONS"

demo "Heartbeat + heavy — eldritch pulse" \
    "$ZALGOCTL" encode '"The heart of darkness beats"' --style heavy --pattern heartbeat --seed "$SEED"

demo "Wave + demonic — rolling madness" \
    "$ZALGOCTL" encode '"Waves of insanity"' --style demonic --pattern wave --seed "$SEED"

demo "Crescendo + extreme + extended blocks" \
    "$ZALGOCTL" encode '"The horror builds"' --style extreme --pattern crescendo --blocks all --seed "$SEED"

demo "Spike + void — center of the storm" \
    "$ZALGOCTL" encode '"The eye sees all"' --style void --pattern spike --seed "$SEED"

demo "Fade-in + letters only — ancient text appearing" \
    "$ZALGOCTL" encode '"The ancient words appear"' --pattern fade-in --marks letters --up 8 --mid 0 --down 0 --seed "$SEED"

demo "Sawtooth + dots — dotted rhythm" \
    "$ZALGOCTL" encode '"dot dot dot dot dot dot"' --pattern sawtooth --marks dots --intensity 12 --wave-freq 3 --seed "$SEED"

demo "Decrescendo + lines — fading strikethrough" \
    "$ZALGOCTL" encode '"Vanishing text"' --pattern decrescendo --marks lines --intensity 20 --seed "$SEED"

demo "Pulse + up-only — intermittent ascension" \
    "$ZALGOCTL" encode '"Up and down and up and down"' --style up-only --pattern pulse --seed "$SEED"

demo "Wave + max-marks 4 — controlled waves" \
    "$ZALGOCTL" encode '"Smooth controlled waves"' --pattern wave --intensity 30 --max-marks 4 --seed "$SEED"

demo "Crescendo + no-skip-spaces — total corruption" \
    "$ZALGOCTL" encode '"Nothing is safe"' --pattern crescendo --intensity 20 --no-skip-spaces --seed "$SEED"

demo "Classic pool + heartbeat — old school pulse" \
    "$ZALGOCTL" encode '"The ancient heart still beats"' --pool classic --pattern heartbeat --intensity 15 --seed "$SEED"

demo "Geometric pool + wave + sparse — glitch transmission" \
    "$ZALGOCTL" encode '"Signal degradation detected in transmission"' --pool geometric --pattern wave --prob 0.4 --intensity 12 --seed "$SEED"

demo "Dense pool + spike — thick center impact" \
    "$ZALGOCTL" encode '"The weight of it all"' --pool dense --pattern spike --intensity 20 --seed "$SEED"

demo "Full pool + sparse + extreme — rare alien artifacts" \
    "$ZALGOCTL" encode '"Strange symbols appear in the manuscript"' --pool full --prob 0.15 --style extreme --seed "$SEED"

demo "Classic + sparse + crescendo — corruption spreading" \
    "$ZALGOCTL" encode '"It starts slow then consumes everything"' --pool classic --prob 0.5 --pattern crescendo --intensity 25 --seed "$SEED"

demo "Geometric + pulse + max-marks — controlled glitch art" \
    "$ZALGOCTL" encode '"Error error normal error error"' --pool geometric --pattern pulse --max-marks 6 --intensity 30 --seed "$SEED"

# ── file output ──────────────────────────────────────────────────────────────

section "FILE OUTPUT"

TMPFILE=$(mktemp)
demo "Write to file with -o" \
    "$ZALGOCTL" encode '"Saved to disk"' --intensity 10 --seed "$SEED" -o "$TMPFILE" '&& echo "Written to '"$TMPFILE"'" && cat '"$TMPFILE"''
rm -f "$TMPFILE"

# ── done ─────────────────────────────────────────────────────────────────────

printf '\n%b━━━ END ━━━%b\n\n' "$MAGENTA$BOLD" "$RESET"
