#!/usr/bin/env bash
# Convert the formal-document Markdown templates to Word .docx
# using slohmaier-doc-template.docx as style reference.
#
# Word is the industry standard for formal release / ISO 9001 audits.
# Markdown remains the source of truth; Word is generated from it.
#
# These templates are converted to Word:
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SRC="$REPO_ROOT/templates"
DST="$REPO_ROOT/templates-word"
TEMPLATE="$DST/slohmaier-doc-template.docx"

if [[ ! -f "$TEMPLATE" ]]; then
  echo "Building base Word template first..."
  python3 "$REPO_ROOT/tools/build_word_template.py"
fi

# Formal documents (to Word):
FORMAL_DOCS=(
  PID-template
  PM-Plan-template
  QA-Plan-template
  SWE-Plan-template
  Test-Plan-template
  Review-Minutes-template
  Non-Conformity-template
  MISRA-Deviation-Permit-template
  MISRA-Deviation-Record-template
  quote-template
)

mkdir -p "$DST"

for doc in "${FORMAL_DOCS[@]}"; do
  if [[ -f "$SRC/$doc.md" ]]; then
    echo "Converting: $doc.md -> $doc.docx"
    pandoc "$SRC/$doc.md" \
      --reference-doc="$TEMPLATE" \
      --toc \
      --toc-depth=3 \
      -o "$DST/$doc.docx"
  else
    echo "WARN: $SRC/$doc.md not found, skipping"
  fi
done

echo ""
echo "Done. Word templates at: $DST/"
ls -la "$DST/"
