#!/usr/bin/env python3
"""임시 실험 5 — 콘티→콘티 자세 체이닝.

S12sh18 경량 콘티를 다시 생성하되, S12sh11 경량 콘티(시신 자세 확정본)를
참조 이미지로 첨부: 부동 인물의 자세·방향을 이전 샷 콘티 이미지로 잠금.
산출: quad3_conti_S12sh18_chained.png (단일 패널, i2).
"""
import sys
from pathlib import Path

HERE = Path(__file__).parent
sys.path.insert(0, str(HERE))
import forest_lib as F  # noqa: E402
import s35_fullrun as R  # noqa: E402
import s37_rooftop_rebuild as W  # noqa: E402
import s34_stillrun as S  # noqa: E402

TAG = "S12sh18"
PREV_CONTI = W.OUT / "quad3_conti_S12sh11.png"
OUTF = W.OUT / "quad3_conti_S12sh18_chained.png"

plan = F.load_plan(W.PLAN)
plan35 = W._s35()
mv = W._movement(plan35)
gmap = {g["key"]: g for g in plan35["groups"]}
place_en = gmap[W.INT_GROUP]["place_en"]

s = R.SHOT[R._key(TAG)]
m = mv.get(TAG, {})
fix = plan.get("pose_fix", {}).get(TAG) or {}
movement = fix.get("movement_en") or m.get("movement", "")
figures = fix.get("figures_en") or m.get("figures", "")
carried = fix.get("carried_en") or (plan.get("int_cont", {})
                                    .get(TAG, {}).get("carried_en", ""))
pose_clauses = [
    "IMMOBILE CHARACTER POSE — CANONICAL (on any conflict THIS POSE"
    " WINS): " + S._soften(it["pose_en"])
    for it in plan.get("pose_canon", []) if TAG in it["shots"]]

LIGHT_STYLE = "\n".join([
    "STYLE — LAYOUT-ONLY THUMBNAIL SKETCH: the lightest possible",
    "director's layout drawing. THIN, CLEAN, UNIFORM PENCIL CONTOUR",
    "LINES ONLY on pure white paper. Absolutely NO shading, NO tone, NO",
    "hatching, NO texture, NO grayscale fill, NO light-and-shadow, NO",
    "facial features beyond a bare head outline, NO clothing detail, NO",
    "material rendering. The panel contains ONLY: the camera framing,",
    "each figure as a simple correct-proportion human contour in its",
    "exact pose and screen position/size, and the few major set masses",
    "as plain outline shapes.",
])

prompt = "\n\n".join([
    "Draw ONE single storyboard layout panel (one shot, full image,"
    " no grid).",
    LIGHT_STYLE,
    "PREVIOUS SHOT LAYOUT SKETCH (attached) — the immediately preceding"
    " shot of the SAME immobile character: her body pose, position and"
    " orientation are LOCKED to that sketch. She cannot move. This new"
    " panel shows a closer framing of THAT SAME unmoved body — every"
    " visible body part (hand, wrist, arm, torso angle) must be"
    " geometrically consistent with the attached sketch, only the"
    " camera changes.",
    f"THE LOCATION: {place_en}",
    "\n".join(pose_clauses) if pose_clauses else "",
    f"THE SHOT (authoritative, Korean): {S._soften(s['desc'])}",
    ("MOVEMENT (follow exactly): " + S._soften(movement))
    if movement else "",
    ("FIGURES — size & depth (follow exactly): " + S._soften(figures))
    if figures else "",
    ("CARRIED STATE (persist exactly): " + S._soften(carried))
    if carried else "",
    "ABSOLUTELY NO TEXT anywhere, no numbers, no captions.",
])

F.img_gpt("x_conti_chain_S12sh18", prompt, refs=[PREV_CONTI],
          size="1024x1024", out_path=OUTF)
print("saved:", OUTF)
