#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""p11a_keyframe_pilot/r003 六段反向填充生图 prompt (v1)

链路: GT 新鲜首尾帧 (rev4 窗口直抽, stale cond 已出身审计作废 MAD49)
  → vision 两帧实读 (2026-09-18 本会话)
  → 六段反向填充 (condition-frame-prompt-v2 schema 逆向)
  → 双臂: A=t2i 纯prompt / B=i2i (TR001 char_001 + SC04 clearing 2ref)
产出: arms.json 记录两臂 prompt 与引擎参数
"""
import json, os

D = os.path.dirname(os.path.abspath(__file__))

# ── vision 实读汇总 (两帧一致的部分进共同段, 差异进 delta 段) ──
COMMON = {
    "shot": "over-the-shoulder close-up at eye level, shallow depth of field",
    "subject": "the rhino beetle warrior (glossy reddish-brown armored shell, large "
               "single curved horn, mandibles, beige bandages on forearms)",
    "foreground": "blurred back of a small orange fluffy character's head on the left "
                  "edge (out of focus foreground framing); two red tomatoes with green "
                  "stems at the bottom, sharp focus",
    "background": "heavily blurred primeval forest clearing with green foliage and "
                  "round white sunlit bokeh circles",
    "light": "soft warm diffused daylight from upper background, glossy highlights on "
             "the shell, bright natural green-brown palette",
    "composition": "beetle head in right two-thirds of frame facing left toward the "
                   "orange character, tomatoes bottom center-right anchoring the base",
}

FIRST_STATE = (
    "the beetle holds a gentle closed-mouth smile, head slightly lowered, "
    "round white eyes wide open looking up-left toward the orange character"
)
LAST_DELTA = (
    "the smile breaks into a wide open-mouthed hearty laugh: mouth wide open showing "
    "upper and lower teeth, cheeks pushed up, eyes narrowed into happy slits looking "
    "down toward the tomatoes, head tilted slightly down"
)

def build_prompt(slot: str) -> str:
    """slot: 'first' | 'last' — 六段模板, FRAME ANCHOR/TERMINAL STATE 互斥"""
    seg = []
    if slot == "first":
        seg.append("[FRAME ANCHOR] this exact moment: gentle smile, head slightly "
                   "lowered, calm and attentive")
        state = FIRST_STATE
        delta = ""
    else:
        seg.append("[TERMINAL STATE] end of a laugh: fully open-mouthed, widest smile "
                   "of the shot, all previous calm gone")
        state = LAST_DELTA
        delta = ""
    seg.append(f"[SUBJECT] {COMMON['subject']}. {state}")
    seg.append(f"[SPATIAL] {COMMON['foreground']}. {COMMON['composition']}")
    seg.append(f"[BACKGROUND] {COMMON['background']}")
    seg.append(f"[LIGHT & COLOR] {COMMON['light']}")
    seg.append("[STYLE] 3D animation, Pixar-level rendering, cinematic shallow depth "
               "of field, 16:9")
    seg.append("[PHYSICS] single static frame, no motion blur, no text, clean image")
    return ". ".join(seg)

arms = {
    "uid": "r003_p1",
    "window": [9.6333, 12.1003],
    "gt_frames": {
        "first": f"{D}/gt_frames/r003_first_gt.jpg",
        "last": f"{D}/gt_frames/r003_last_gt.jpg",
    },
    "note": "stale cond_units_full cond_last 作废(出身审计 MAD=49.2>3.0); GT 用 rev4 窗口新鲜直抽",
    "arms": {
        "A_t2i": {
            "engine": "dreamina text2image 4.6 2k 16:9",
            "prompts": {"first": build_prompt("first"), "last": build_prompt("last")},
        },
        "B_i2i": {
            "engine": "dreamina image2image 4.6 2k 16:9, refs=[char_001 TR v13c, SC04 clearing]",
            "refs": [
                "/data/workspace/kais-gold-remount/episodes/ep-xiaojianghu-ep01/S4_closures/p04_tr_batch/char_001_tr_v13c_qwenedit_s941.png",
                "/data/workspace/kais-gold-remount/episodes/ep-xiaojianghu-ep01/S4_closures/p07_scenes/SC04_forest_clearing.png",
            ],
            "ref_rationale": "ref-priority 纪律: TR 提供身份锚, SC04 提供环境锚; 禁加首帧 ref 防毒害",
            "prompts": {"first": build_prompt("first"), "last": build_prompt("last")},
        },
    },
}
out = f"{D}/arms.json"
json.dump(arms, open(out, "w"), ensure_ascii=False, indent=1)
print("arms.json written")
print("\n=== FIRST prompt ===")
print(build_prompt("first"))
print("\n=== LAST prompt ===")
print(build_prompt("last"))
