# justfile — Build orchestration for *ki-buch* PDF
#
# Portiert von ki-hinterm-vorhang (just + assemble + template + Lua-Filter),
# ergänzt um ki-buch-spezifisches Mermaid-/Callout-Preprocessing.
#
# Requires: pandoc, lualatex (TeX Live), python3
# Optional: mmdc/npx (Mermaid), just (this file)

default: pdf

# --- Preprocess: Mermaid → PNG, Callouts → LaTeX, unter build/src/ ----------
preprocess:
    #!/usr/bin/env bash
    set -euo pipefail
    ROOT="$(pwd)"
    BUILD="$ROOT/build"
    PRE_ARGS=(python3 "$BUILD/preprocess_mermaid.py" --book-root "$ROOT" --build-dir "$BUILD")
    if [[ "${SKIP_MERMAID:-0}" == "1" ]]; then
      PRE_ARGS+=(--skip-render)
    fi
    echo "==> Mermaid preprocess…"
    "${PRE_ARGS[@]}"
    echo "==> Callout typing…"
    python3 "$BUILD/preprocess_callouts.py" --src-dir "$BUILD/src"

# --- Assemble: build/src (falls vorhanden) oder Quellen → build/book.md -----
assemble:
    @python3 scripts/assemble_book.py

# --- Full PDF (styled, Lua filters, Deep Signal palette) --------------------
pdf-styled: preprocess assemble
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p dist
    echo "==> Pandoc → PDF (LuaLaTeX)…"
    pandoc build/book.md \
        --from=markdown+smart+pipe_tables+table_captions+fenced_code_attributes+link_attributes+raw_attribute+header_attributes-implicit_figures \
        --pdf-engine=lualatex \
        --pdf-engine-opt=-interaction=nonstopmode \
        --template=pdf/templates/book.tex \
        -H pdf/styles/colors.tex \
        --metadata-file=pdf/metadata.yaml \
        --lua-filter=pdf/filters/boxes.lua \
        --lua-filter=pdf/filters/figures.lua \
        --lua-filter=pdf/filters/illustrations.lua \
        --lua-filter=pdf/filters/partpages.lua \
        --resource-path=.:bilder:assets:build:build/src \
        -V lang=de-DE -V papersize=a4 -V colorlinks \
        -V toc-title=Inhaltsverzeichnis \
        --toc --toc-depth=2 \
        -o dist/agentenbasierte-ki-se.pdf \
        2> build/pandoc.log || {
          echo "ERROR: pandoc failed. Last 80 lines of build/pandoc.log:"
          tail -n 80 build/pandoc.log || true
          exit 1
        }
    SIZE=$(wc -c < dist/agentenbasierte-ki-se.pdf | tr -d ' ')
    PAGES=$(pdfinfo dist/agentenbasierte-ki-se.pdf 2>/dev/null | awk '/^Pages:/ {print $2}' || echo "?")
    echo "==> Done: dist/agentenbasierte-ki-se.pdf ($SIZE bytes, ~$PAGES pages)"

pdf: pdf-styled

# Draft: same pipeline, draft flag (no mermaid re-render by default)
pdf-draft:
    #!/usr/bin/env bash
    set -euo pipefail
    export SKIP_MERMAID="${SKIP_MERMAID:-1}"
    just preprocess
    just assemble
    mkdir -p dist
    pandoc build/book.md \
        --from=markdown+smart+pipe_tables+table_captions+fenced_code_attributes+link_attributes+raw_attribute+header_attributes-implicit_figures \
        --pdf-engine=lualatex \
        --pdf-engine-opt=-interaction=nonstopmode \
        --template=pdf/templates/book.tex \
        -H pdf/styles/colors.tex \
        --metadata-file=pdf/metadata.yaml \
        --lua-filter=pdf/filters/boxes.lua \
        --lua-filter=pdf/filters/figures.lua \
        --lua-filter=pdf/filters/illustrations.lua \
        --lua-filter=pdf/filters/partpages.lua \
        --resource-path=.:bilder:assets:build:build/src \
        -V lang=de-DE -V papersize=a4 -V colorlinks \
        -V toc-title=Inhaltsverzeichnis \
        -V draft=true \
        --toc --toc-depth=2 \
        -o dist/agentenbasierte-ki-se-draft.pdf \
        2> build/pandoc.log || {
          echo "ERROR: pandoc failed. Last 80 lines of build/pandoc.log:"
          tail -n 80 build/pandoc.log || true
          exit 1
        }
    echo "==> Draft: dist/agentenbasierte-ki-se-draft.pdf"

# Quick sample: Vorwort + Einleitung + Kap. 1 (no mermaid preprocess)
pdf-sample:
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p dist
    pandoc \
        frontmatter/vorwort.md \
        frontmatter/einleitung.md \
        teil-01-grundlagen/kapitel-01-evolution-sprachmodelle.md \
        --from=markdown+smart+pipe_tables+table_captions+fenced_code_attributes+link_attributes+raw_attribute+header_attributes-implicit_figures \
        --pdf-engine=lualatex \
        --pdf-engine-opt=-interaction=nonstopmode \
        --template=pdf/templates/book.tex \
        -H pdf/styles/colors.tex \
        --metadata-file=pdf/metadata.yaml \
        --lua-filter=pdf/filters/boxes.lua \
        --lua-filter=pdf/filters/partpages.lua \
        --resource-path=.:bilder:assets \
        -V lang=de-DE -V papersize=a4 -V colorlinks \
        -V toc-title=Inhaltsverzeichnis \
        --toc --toc-depth=2 \
        -o dist/ki-se-sample.pdf \
        2> build/pandoc-sample.log || {
          echo "ERROR: sample build failed. Last 80 lines:"
          tail -n 80 build/pandoc-sample.log || true
          exit 1
        }
    echo "==> Sample: dist/ki-se-sample.pdf"

# Post-build QA (eBook)
qa:
    @python3 scripts/check_pdf.py --mode ebook

all: pdf qa

# --- KDP Paperback Interior (6×9, grayscale) --------------------------------

_print_resource_path := "build/print-assets:.:bilder:assets:build:build/src"

print-assets:
    @python3 scripts/prepare_print_assets.py

pdf-print: preprocess assemble print-assets
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p dist
    echo "==> Pandoc → Print PDF (KDP 6×9 Grayscale)…"
    pandoc build/book.md \
        --from=markdown+smart+pipe_tables+table_captions+fenced_code_attributes+link_attributes+raw_attribute+header_attributes-implicit_figures \
        --pdf-engine=lualatex \
        --pdf-engine-opt=-interaction=nonstopmode \
        --template=pdf/templates/book.tex \
        -H pdf/print/print-colors.tex \
        --metadata-file=pdf/print/metadata.yaml \
        --lua-filter=pdf/filters/boxes.lua \
        --lua-filter=pdf/filters/figures.lua \
        --lua-filter=pdf/filters/illustrations.lua \
        --lua-filter=pdf/filters/partpages.lua \
        --resource-path={{_print_resource_path}} \
        --syntax-highlighting=monochrome \
        -V lang=de-DE \
        -V printmode=true \
        -V toc-title=Inhaltsverzeichnis \
        --toc --toc-depth=1 \
        -o dist/agentenbasierte-ki-se-print.pdf \
        2> build/pandoc-print.log || {
          echo "ERROR: print pandoc failed. Last 80 lines of build/pandoc-print.log:"
          tail -n 80 build/pandoc-print.log || true
          exit 1
        }
    SIZE=$(wc -c < dist/agentenbasierte-ki-se-print.pdf | tr -d ' ')
    PAGES=$(pdfinfo dist/agentenbasierte-ki-se-print.pdf 2>/dev/null | awk '/^Pages:/ {print $2}' || echo "?")
    echo "==> Done: dist/agentenbasierte-ki-se-print.pdf ($SIZE bytes, ~$PAGES pages)"

pdf-print-optimize:
    @test -f dist/agentenbasierte-ki-se-print.pdf || (echo "Run just pdf-print first" && exit 1)
    @command -v gs >/dev/null || (echo "Ghostscript (gs) not found" && exit 1)
    gs -dBATCH -dNOPAUSE -dSAFER -sDEVICE=pdfwrite \
        -dCompatibilityLevel=1.5 \
        -dPDFSETTINGS=/prepress \
        -dColorConversionStrategy=/Gray \
        -dProcessColorModel=/DeviceGray \
        -dEmbedAllFonts=true \
        -dSubsetFonts=true \
        -sOutputFile=dist/agentenbasierte-ki-se-print-opt.pdf \
        dist/agentenbasierte-ki-se-print.pdf
    @mv dist/agentenbasierte-ki-se-print-opt.pdf dist/agentenbasierte-ki-se-print.pdf
    @echo "optimized → dist/agentenbasierte-ki-se-print.pdf"

qa-print:
    @python3 scripts/check_pdf.py --mode print

all-print: pdf-print qa-print

# Assemble only (no preprocess)
assemble-raw:
    @python3 scripts/assemble_book.py

clean:
    @rm -f build/book.md build/pandoc.log build/pandoc-sample.log build/pandoc-print.log
    @rm -rf build/print-assets
    @rm -f dist/*.pdf
    @echo "cleaned build/book.md, logs, print-assets, and dist/*.pdf"

# Full clean including preprocessed mermaid cache
clean-all: clean
    @rm -rf build/src
    @echo "also removed build/src (re-run mermaid on next pdf)"
