"""S12 라인아트 v2 — 커튼 유지 + 멀티턴 참조."""
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 pure black background, flat 2D diagram style like an architectural blueprint fused with storyboard sketch. No fill, no shading, no texture, no photorealism — only colored vector outlines and joint dots.

Setting: interior of a cramped Korean rooftop room (옥탑방). The room has a partitioning curtain that divides the bed/sleeping area from the rest of the room. The camera is placed on the non-bed side of the room at eye level, looking across toward the bed area through the partitioning curtain.

Background architecture in thin gray lines (#888888): cramped walls on left and right defining a narrow corridor-like space, floor line with perspective guides converging toward the far end. The ceiling can be implied by a top line.

The partitioning curtain hangs from ceiling to floor across the full frame width, drawn in thin white lines (#FFFFFF). It has just been pulled open in the middle, so both halves hang parted to the left and right like opened stage drapes, each half taking up roughly a third of the frame width on each side. The fabric falls vertically with gentle wave folds drawn as curved white lines. The opening between them reveals the bed area behind.

Beyond the parted curtain, in the center-right midground: the bed drawn in thin white lines along the right wall, mattress edge and pillow area readable. The bed is slightly angled in perspective because the camera is not perfectly square.

Character 1 in cyan (#00E5FF) stands just in front of the curtain opening in the left-midframe, full body visible as a colored outline silhouette with joint dots. Her torso is angled three-quarter left toward the camera, but her head and gaze are sharply directed to the right across the bed area toward the corpse. One of her cyan hands is still touching the edge of the left curtain half, as if she has just pulled it open. She is frozen mid-motion.

Character 2 in magenta (#FF00FF) sits slumped on the floor near the bed and wall in the right-midground, behind the opened curtain, visible through the opening. Her head is bowed down lifelessly. Her shoulder and collarbone are drawn with torn contour damage in jagged magenta lines with red dotted blood flowing from the wounds. A red (#FF0000) tattoo mark is visible on her left wrist. One of her lifeless magenta hands loosely holds a crumpled photograph drawn as a small orange (#FFA500) geometric outline. Both her arms hang limply along her torso, her posture completely motionless.

On the floor between the curtain and the corpse: thick red (#FF0000) blood pools and smeared bloody footprints drawn as red dots and dashed trails leading diagonally from the foreground to character 2.

Camera framing: eye-level medium-wide shot, the parted curtain framing the discovery on both sides of the frame, character 1 in left-midframe and character 2 in right-midground aligned on a single discovery axis.

Colors summary: cyan #00E5FF for character 1, magenta #FF00FF for character 2, orange #FFA500 for the photograph, red #FF0000 for all blood and wrist mark, white #FFFFFF for curtain and bed, gray #888888 for walls and floor, pure black background."""

# Shot 2 — 같은 방, 커튼 그대로 유지, 카메라가 방 안으로 이동
SHOT_2_CONTINUATION = """Continue the exact same line drawing style, color palette, entities, and room layout from the previous reference image. Same cramped Korean rooftop room (옥탑방), same partitioning curtain, same bed, same corpse in identical pose. This is a closer canted second shot in the same continuous two-shot sequence.

KEEP IDENTICAL from the reference image (do NOT change these):
- The partitioning curtain in thin white lines, still parted open in the middle. It is NOT removed. Both halves still hang from ceiling to floor, pulled to the left and right. From the new camera position inside the bed area, the curtain is now seen from slightly behind/beside, so it frames the LEFT edge and the FAR BACKGROUND of the frame instead of both sides.
- The bed in thin white lines along the right wall, same orientation.
- The magenta character 2 in the exact same slumped position near the bed and wall, head bowed, arms hanging limp along her torso, the crumpled orange photograph in her hand, same red tattoo on left wrist, same mutilated shoulder and collarbone with red dotted blood.
- The red blood pools and dotted footprint trails on the floor in the same positions.
- The gray wall and floor line work, the pure black background.

CHANGES for this closer canted second shot:
- Camera moves INSIDE the bed area, now standing beside the bed with a slight canted (tilted) angle. The room tilts slightly so the bed line, wall line, and curtain line all slant diagonally.
- The cyan character 1 has also stepped through the curtain opening and now stands on the LEFT side of the frame inside the bed area, recoiling backward, shoulders pulled back, arms tense with one cyan hand still extended back toward the curtain as if anchoring herself. Her gaze is lifted UP toward the wall above the bed (not toward the corpse).
- On the upper center wall above the bed, a new supernatural incomplete red (#FF0000) circular mark appears, made of wet-looking red dotted and brushed line segments, as if painted by an invisible brush. Keep this mark visually dominant in the upper center.
- The parted curtain is still fully visible: one half of it drapes down along the left edge of the frame in the foreground (closer to camera because we stepped through), while the other half is seen across the frame to the right-background where it meets the far wall.

Frame composition for this canted shot: corpse anchors the lower right, the red wall circle dominates the upper center above the bed, cyan character 1 on the left mid-foreground, the curtain still framing the left edge and far background so the room's spatial structure stays readable.

Same line art rules: colored vector outlines with joint dots only, no fill, no shading, no photorealism. Same color palette as the reference image."""


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

    # ── Shot 1: 독립 생성 ──
    print("\n→ generating shot 1 v2 (independent, curtain emphasized)...")
    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_v2.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 v2 (with shot 1 as reference, curtain preserved)...")
    labeled_refs = [("Previous shot 1 reference — preserve style, layout, curtain, bed, corpse:", 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_v2.png"
    shot2_path.write_bytes(shot2_bytes)
    print(f"  ✓ saved {shot2_path.name} ({len(shot2_bytes)} bytes, {ms2}ms)")


if __name__ == "__main__":
    main()
