#!/usr/bin/env bash
# add-placeholders.sh — Bash-only, inserts image placeholder blocks after headings.
set -euo pipefail

DECK="/home/mnm/workspaces/thebaker/assets/decks/THE_BAKER_Cannes_Deck.typ"
BAK="${DECK}.bak"
cp "$DECK" "$BAK"

placeholder() {
    local w="$1" h="$2" label="$3"
    cat <<BLOCK
#v(10pt)
#block(width: ${w}%, height: ${h}mm, stroke: 1pt + c_line, radius: 4pt, fill: c_card2)[
  #align(center + horizon)[
    #text(fill: c_muted, style: "italic")[Illustration: ${label}]
  ]
]
BLOCK
}

# We'll build a sed script that inserts after the first display/h1/h2 in each slide.
# Strategy: use line numbers. Find each slide's heading line, insert placeholder after it.
# But we need to do this from bottom to top so line numbers don't shift.

# First, find all slide heading lines and their numbers
tmp=$(mktemp)
grep -n '#display\[\|#h1\[\|#h2\[' "$DECK" | while IFS=: read -r lineno rest; do
    echo "$lineno"
done > "$tmp.headings"

# Now build insertion commands (bottom-up)
# We need to know which slide each heading belongs to.
# Simpler approach: just do a sed replacement that adds a marker after each display/h1/h2,
# but only the FIRST one in each slide.

# Actually, let's use a different approach: 
# For each slide, find its first display/h1/h2 line and insert placeholder after it.
# We track slides by "// SLIDE" / "// ANNEX" markers.

awk -v tmp="$tmp" '
BEGIN {
    placeholder_tpl = "#v(10pt)\n#block(width: %s%%, height: %smm, stroke: 1pt + c_line, radius: 4pt, fill: c_card2)[\n  #align(center + horizon)[\n    #text(fill: c_muted, style: \"italic\")[Illustration: %s]\n  ]\n]\n"
    
    # Specs per slide number
    specs[1]  = "100|80|Cover Illustration Area"
    specs[2]  = "100|48|Triptych—bakery bread rack, property skyline at golden hour, and a bloodied hand clutching a rosary over a ledger"
    specs[3]  = "100|58|Fredric in the Vatican Room, diminished patriarch surrounded by photographs, pills, and a pistol on velvet cloth—deep chiaroscuro"
    specs[4]  = "100|62|Carved wooden family crest half-covered in shadow, a bullet casing and a communion wafer resting beside it on marble"
    specs[5]  = "100|58|Fishing caique leaving Byblos at dawn, flames on the horizon, a crucifix slipping beneath dark Mediterranean water"
    specs[6]  = "100|44|Wide interior of the Barakat estate—food, photographs, guns, and church ritual objects sharing a single marble table, Baroque chiaroscuro"
    specs[7]  = "100|52|Fredric seated in the Vatican Room chair, family photographs behind him, a pistol and pill bottles on a side table, face half-lit"
    specs[8]  = "100|46|Three-panel sequence—mountain wedding escape, Holy Communion in white, and a bloodied kitchen knife on a cutting board"
    specs[9]  = "100|50|Split frame—left: Isabella praying in Saint Charbel; right: Billy in a nightclub basement, fist raised, both lit by single harsh light sources"
    specs[10] = "100|48|Baroque Catholic altar melting into a nightclub basement, a family portrait hanging between them, light carrying dust and gold"
    specs[11] = "100|55|Monsignor and Fredric in Saint Charbel confessional light, an envelope of church money visible, crucifix catching a gold beam"
    specs[12] = "100|50|Four heirs in chiaroscuro grid—Billy, Aida, Vincent, Nancy—each caught in a different quality of shadow and gold light"
    specs[13] = "100|58|Magda in the kitchen at night, knife and rosary sharing a cutting board, face half in shadow, mourning becoming command"
    specs[14] = "100|76|Director portrait—Nicholas Lathouris in natural light, marked-up screenplay in hand, surrounded by production stills and theatre posters"
    specs[15] = "100|76|Writer-producer portrait—Ronny Mouawad in a Sydney bakery at dawn, flour on his hands, a script beside him, warm tungsten glow"
    specs[16] = "100|42|Producers' world—Sydney bakery interior, Cannes palace facade, and Beirut harbour at dusk, composited in a triptych of warm golds"
    specs[17] = "100|46|Cast mood board—six faces in chiaroscuro grid, patriarch, matriarch, and heirs caught between ritual and violence"
    specs[18] = "100|64|A diaspora family in a darkened cinema, faces lit by screen glow, an older patriarch seated among younger generations"
    specs[19] = "100|56|Stylized world map highlighting Core cities—Sydney, Melbourne, Paris, Montreal, Toronto, Dearborn, Beirut, Los Angeles, São Paulo—each marked with a gold dot"
    specs[20] = "100|44|A locked room door with light seeping through the jamb, a family name carved in the wood, half-burned letters on a desk"
    specs[21] = "100|42|Four arched windows—Cannes palace, Beirut port memorial, Maronite church interior, and Australian film-awards stage—each with different light"
    specs[22] = "100|40|A ledger book open on a marble table, a loaf of bread and a rosary resting on the pages, golden side-light"
    specs[23] = "100|40|Abstract Australian landscape—red earth and gold light—overlaid with subtle ledger lines"
    specs[24] = "100|42|Four stacked stone layers representing finance sources, each layer catching a different quality of light"
    specs[25] = "100|38|Six chairs around a dark table, each under a single spotlight, papers and pens waiting"
    specs[26] = "100|42|Four roads converging on a distant estate, each from a different direction—Sydney, Paris, Beirut, and a sales floor"
    specs[27] = "100|42|Seed, Measure, Move as three overlapping circles of light in a dark room, each brighter than the last"
    specs[28] = "100|38|The Cannes Croisette redrawn as a house floor plan, meeting rooms marked as estate rooms, gold lines on dark paper"
    specs[29] = "100|62|The Barakat estate at night, a single lit window, the garden in shadow, the house remembering"
    specs[30] = "220|50|Cannes Palais des Festivals facade at night, gold light on dark stone"
    specs[31] = "100|50|A producer's desk—QAPE spreadsheet, Australian flag, and a coffee cup under a desk lamp"
    specs[32] = "100|46|Screen Australia and Screen NSW logos as architectural pillars holding up a film strip"
    specs[33] = "100|46|France and Lebanon as two coastlines joined by a bridge of light over water"
    specs[34] = "100|46|A world map made of sales territory flags, each pinned with a gold dot"
    specs[35] = "100|46|Six document folders on a dark table, each labeled with a partner territory, a single pen beside them"
    specs[36] = "100|46|A trailer edit suite—three monitors showing Communion, Cash, and Magda'\''s kitchen, the editor'\''s hand on a joystick"
    
    slide_num = 0
    found_heading = 0
}

/^\/\/ (SLIDE|ANNEX) / {
    slide_num++
    found_heading = 0
}

/^[[:space:]]*#(display|h1|h2)\[/ {
    if (found_heading == 0 && slide_num in specs) {
        found_heading = 1
        # Check if slide already has an illustration
        # (we track this by looking ahead, but simpler to just check in the current text)
        split(specs[slide_num], parts, "|")
        w = parts[1]; h = parts[2]; label = parts[3]
        
        printf "%s", $0
        printf "\n"
        printf "#v(10pt)\n"
        printf "#block(width: %s%%, height: %smm, stroke: 1pt + c_line, radius: 4pt, fill: c_card2)[\n", w, h
        printf "  #align(center + horizon)[\n"
        printf "    #text(fill: c_muted, style: \"italic\")[Illustration: %s]\n", label
        printf "  ]\n"
        printf "]\n"
        next
    }
}

{ print }
' "$DECK" > "${DECK}.new"

mv "${DECK}.new" "$DECK"
echo "Done. Original backed up to $BAK"
wc -l "$DECK"
