#!/bin/bash
# capture-fixture.sh — Extract agent output fixture from a document
# Usage: bash tests/capture-fixture.sh <source-file> <section-header> <output-dir>
#
# Example:
#   bash tests/capture-fixture.sh docs/utredning/.../utredning.md "S5: Sikkerhetsvurdering" tests/fixtures/security-assessment/
set -euo pipefail

if [ $# -lt 3 ]; then
  echo "Usage: $0 <source-file> <section-header> <output-dir>"
  echo ""
  echo "Extracts a section from source file and saves as fixture.md"
  echo ""
  echo "Example:"
  echo "  $0 docs/utredning/file.md 'S5: Sikkerhetsvurdering' tests/fixtures/security-assessment/"
  exit 1
fi

SOURCE="$1"
HEADER="$2"
OUTPUT_DIR="$3"

if [ ! -f "$SOURCE" ]; then
  echo "Error: Source file not found: $SOURCE"
  exit 1
fi

mkdir -p "$OUTPUT_DIR"

# Extract section from ## header to next ## header (or EOF)
awk -v header="$HEADER" '
  BEGIN { found=0 }
  /^## / {
    if (found) exit
    if (index($0, header)) found=1
  }
  found { print }
' "$SOURCE" > "$OUTPUT_DIR/fixture.md"

LINES=$(wc -l < "$OUTPUT_DIR/fixture.md" | tr -d ' ')
echo "Captured $LINES lines to $OUTPUT_DIR/fixture.md"
