"""S12 라인아트 멀티턴 생성 — Shot 1을 참조로 Shot 2 생성."""
import sys
from pathlib import Path

BACKEND = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(BACKEND))

from app.modules.llm.gemini_image_client import GeminiImageClient  # noqa: E402

OUTPUT_DIR = BACKEND / "scripts" / "output" / "line_art_s12"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

# Shot 1 프롬프트 (독립 생성) — 기존과 동일
SHOT_1 = """Pure line drawing on black background, flat 2D diagram style like an architectural blueprint fused with storyboard sketch, with background architecture in thin gray wall lines and thin white furniture lines only. Show the small bedroom from inside the room at curtain level, using a slightly eye-level medium-wide perspective. Parted curtains hang in the extreme near foreground on both left and right edges like torn drapes framing the scene. The bed is drawn in thin white lines along the right wall, its mattress edge and head-side orientation clearly readable, with visible wall space above and behind it to establish the room. Character 1 in cyan stands just beyond the opened curtains in the left-midframe, full body visible as a colored outline silhouette with joint dots, frozen upright after pulling the curtain apart, torso angled three-quarter left, head and gaze directed sharply down and across the room toward the corpse. Character 2 in magenta sits slumped on the floor near the bed and wall in the right-midground, her head bowed down lifelessly. Her shoulder and collarbone are severely mutilated with torn contour damage, resembling jagged bite wounds, with dark red dotted blood flowing from the wounds. A red tattoo mark is visible on her left wrist, and one of her lifeless hands loosely holds a crumpled photograph drawn as a small orange geometric outline. Both her arms hang limply along her torso, her posture completely motionless. Draw thick blood pools and smeared footprints as red dots and dotted trails across the floor, beginning in the lower foreground and leading diagonally toward character 2. Keep all shapes as colored vector lines only, no fill, no shading, no texture, no photorealism. Colors: cyan #00E5FF character 1, magenta #FF00FF character 2, orange #FFA500 key prop outlines, red #FF0000 blood and marks, white #FFFFFF furniture lines, gray #888888 wall and floor lines, pure black background."""

# Shot 2 프롬프트 (Shot 1을 참조로) — 연속성 강조, 변경점만 명시
SHOT_2_CONTINUATION = """Continue the exact same line drawing style, color palette, and room layout from the previous reference image. This is a closer canted angle shot in the same continuous two-shot sequence, same bedroom, same corpse in identical pose.

KEEP IDENTICAL from the reference image:
- The magenta character 2 in the exact same slumped position near the bed and wall
- Her head bowed, arms hanging limp, the crumpled orange photograph in her hand
- Her mutilated shoulder and collarbone with the same red wound pattern
- The red tattoo on her left wrist
- The bed drawn in thin white lines along the wall
- The gray wall and floor line work
- The red blood dots and footprint trails on the floor
- Black background

CHANGES for this closer canted second shot:
- Camera moves closer beside the bed with a slight canted (tilted) angle, so the bed line and wall line slant slightly diagonally
- The cyan character 1 now appears on the LEFT side of the frame, recoiling backward, shoulders pulled back, gaze lifted UP toward the wall above the bed (not toward the corpse anymore)
- On the upper center wall above the bed, a new supernatural incomplete red circular mark appears, made of wet-looking red dotted and brushed line segments, as if painted by an invisible brush
- The curtains from shot 1 are now off to the left background, barely visible
- Frame composition: corpse anchors lower right, red wall circle dominates upper center, cyan character 1 on left

Same line art rules: colored vector outlines with joint dots only, no fill, no shading, no photorealism."""


def main() -> None:
    client = GeminiImageClient()
    client.set_context(step="line_art_multiturn", operation_type="line_art_multiturn_s12")

    # ── Shot 1: 독립 생성 ──
    print("\n→ generating shot 1 (independent)...")
    shot1_bytes, ms1 = client.generate_image(prompt=SHOT_1, aspect_ratio="16:9")
    if not shot1_bytes:
        print("  ✗ shot 1 failed")
        return
    shot1_path = OUTPUT_DIR / "s12_shot1_multiturn.png"
    shot1_path.write_bytes(shot1_bytes)
    print(f"  ✓ saved {shot1_path.name} ({len(shot1_bytes)} bytes, {ms1}ms)")

    # ── Shot 2: Shot 1 참조 ──
    print("\n→ generating shot 2 (with shot 1 as reference)...")
    labeled_refs = [("Previous shot (shot 1) — reference for style and layout continuity:", shot1_bytes)]
    shot2_bytes, ms2 = client.generate_image(
        prompt=SHOT_2_CONTINUATION,
        labeled_references=labeled_refs,
        aspect_ratio="16:9",
    )
    if not shot2_bytes:
        print("  ✗ shot 2 failed")
        return
    shot2_path = OUTPUT_DIR / "s12_shot2_multiturn.png"
    shot2_path.write_bytes(shot2_bytes)
    print(f"  ✓ saved {shot2_path.name} ({len(shot2_bytes)} bytes, {ms2}ms)")


if __name__ == "__main__":
    main()
