#!/bin/bash
# NERDLETTER NUMBERS STATION MP4 GENERATOR
# Lincolnshire Poacher style with letter-by-letter cipher readout
#
# Audio structure:
#   Base layer (full duration): low-volume shortwave static
#   On top, in sequence:
#     1. Lincolnshire Poacher theme
#     2. Voice: announcement + letters (0.5s between letters, 1.0s between groups)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ASSETS_DIR="${SCRIPT_DIR}/../assets"

MESSAGE="$1"          # e.g. "HLXIB JACJP ERXHN EFM"
PAGE="$2"             # e.g. 001
EPISODE="${3:-01}"

OUTFILE="nerdletter_secret_${EPISODE}_page${PAGE}.mp4"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

SAMPLE_RATE=22050

echo "=== NERDLETTER SECRET TRANSMISSION #${EPISODE} ==="
echo "Page ${PAGE} — Generating MP4..."

# --- 1. Generate voice announcement ---
INTRO_TEXT="NERDLETTER SECRET MESSAGE ${EPISODE}. USE DAY BOOK PAGE ${PAGE}."
echo "$INTRO_TEXT" | \
  espeak-ng -ven+f3 -s88 -k50 -a 200 -w "${TMPDIR}/intro.wav" --punct="" 2>/dev/null

# --- 2. Generate letter-by-letter audio with proper gaps ---
# Build an ffmpeg concat list: each letter as a wav, with silence between
CONCAT_LIST="${TMPDIR}/concat.txt"
> "$CONCAT_LIST"
PART=0

# Pre-generate silence files
ffmpeg -f lavfi -i "anullsrc=r=${SAMPLE_RATE}:cl=mono" \
  -t 0.45 -c:a pcm_s16le "${TMPDIR}/gap_letter.wav" -y 2>/dev/null
ffmpeg -f lavfi -i "anullsrc=r=${SAMPLE_RATE}:cl=mono" \
  -t 0.9 -c:a pcm_s16le "${TMPDIR}/gap_group.wav" -y 2>/dev/null

for group in $MESSAGE; do
    echo "  Group: $group"
    for ((i=0; i<${#group}; i++)); do
        letter="${group:$i:1}"
        PART_FILE="${TMPDIR}/letter_${PART}.wav"
        echo "$letter" | espeak-ng -ven+f3 -s81 -k65 -a 210 -w "$PART_FILE" 2>/dev/null
        echo "file '${PART_FILE}'" >> "$CONCAT_LIST"
        PART=$((PART + 1))

        # 0.5s gap after each letter (except last letter of last group)
        if [ $i -lt $((${#group} - 1)) ]; then
            echo "file '${TMPDIR}/gap_letter.wav'" >> "$CONCAT_LIST"
        fi
    done
    # 1.0s gap after each group
    echo "file '${TMPDIR}/gap_group.wav'" >> "$CONCAT_LIST"
done

# Concatenate all letter parts into one track
ffmpeg -f concat -safe 0 -i "$CONCAT_LIST" \
  -c:a pcm_s16le "${TMPDIR}/letters.wav" -y 2>/dev/null

# --- 3. Generate closing announcement ---
OUTRO_TEXT="END TRANSMISSION. TUNE TO OLD COMPUTER NERD DOT COM FOR FURTHER INSTRUCTIONS."
echo "$OUTRO_TEXT" | \
  espeak-ng -ven+f3 -s88 -k50 -a 200 -w "${TMPDIR}/outro.wav" --punct="" 2>/dev/null

# --- 4. Concatenate intro + letters + outro ---
VOICE_LIST="${TMPDIR}/voice_concat.txt"
echo "file '${TMPDIR}/intro.wav'" > "$VOICE_LIST"
echo "file '${TMPDIR}/gap_group.wav'" >> "$VOICE_LIST"
echo "file '${TMPDIR}/letters.wav'" >> "$VOICE_LIST"
echo "file '${TMPDIR}/gap_group.wav'" >> "$VOICE_LIST"
echo "file '${TMPDIR}/outro.wav'" >> "$VOICE_LIST"

ffmpeg -f concat -safe 0 -i "$VOICE_LIST" \
  -ar ${SAMPLE_RATE} -ac 1 -c:a pcm_s16le "${TMPDIR}/voice.wav" -y 2>/dev/null

# --- 5. Concatenate poacher theme + voice into foreground track ---
FG_LIST="${TMPDIR}/fg_concat.txt"
echo "file '${TMPDIR}/poacher_resampled.wav'" > "$FG_LIST"
echo "file '${TMPDIR}/voice.wav'" >> "$FG_LIST"

# Resample poacher intro to match
ffmpeg -i "${ASSETS_DIR}/poacher_intro.wav" \
  -ar ${SAMPLE_RATE} -ac 1 -c:a pcm_s16le "${TMPDIR}/poacher_resampled.wav" -y 2>/dev/null

ffmpeg -f concat -safe 0 -i "$FG_LIST" \
  -c:a pcm_s16le "${TMPDIR}/foreground.wav" -y 2>/dev/null

# --- 6. Get foreground duration, generate static to match ---
FG_DUR=$(ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${TMPDIR}/foreground.wav")
echo "  Audio duration: ${FG_DUR}s"

ffmpeg -f lavfi -i "anoisesrc=d=${FG_DUR}:c=0.22" \
  -af "highpass=f=2500,lowpass=f=9000,volume=0.18" \
  -ar ${SAMPLE_RATE} -ac 1 -t "${FG_DUR}" \
  -c:a pcm_s16le "${TMPDIR}/static.wav" -y 2>/dev/null

# --- 7. Mix static underneath foreground ---
ffmpeg -i "${TMPDIR}/foreground.wav" -i "${TMPDIR}/static.wav" \
  -filter_complex "[0:a][1:a]amix=inputs=2:duration=longest:normalize=0" \
  -c:a pcm_s16le "${TMPDIR}/final_audio.wav" -y 2>/dev/null

# --- 8. Build video to match audio duration ---
# Round up to nearest second for video duration
VIDEO_DUR=$(echo "$FG_DUR" | awk '{printf "%d", $1 + 1}')

ffmpeg -f lavfi -i "color=black:s=1280x720:d=${VIDEO_DUR}" \
  -i "${TMPDIR}/final_audio.wav" \
  -vf "drawtext=font='Courier':fontsize=50:fontcolor=green@0.95:text='NERDLETTER SECRET TRANSMISSION #${EPISODE}':x=(w-text_w)/2:y=80, \
drawtext=font='Courier':fontsize=36:fontcolor=green@0.85:text='USE DAY BOOK PAGE ${PAGE}':x=(w-text_w)/2:y=170, \
drawtext=font='Courier':fontsize=44:fontcolor=lime@0.95:text='${MESSAGE}':x=(w-text_w)/2:y=270:box=1:boxcolor=black@0.75:boxborderw=15, \
drawtext=font='Courier':fontsize=30:fontcolor=green@0.75:text='DESTROY PAGE AFTER USE — SUBSCRIBERS ONLY':x=(w-text_w)/2:y=640" \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest "$OUTFILE" -y

echo "Done: ${OUTFILE}"
