#!/usr/bin/env python3
"""임시 실험 2 — quad_s12.png 4분할 크롭 → 패널별 '사실화만 극대화' i2i.

각 패널을 유일 참조로 붙이고, 프롬프트는 '포토리얼리즘 극대화, 나머지
전부 절대 불변'만 지시. 산출 4장 = quad_refined_<tag>.png.
"""
import sys
from pathlib import Path

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

TAGS = ["S12sh11", "S12sh18", "S12sh21", "S14sh5"]
SRC = W.OUT / "quad_s12.png"

# ① 등분 크롭 (2x2, 거터 정확 50% — 생성 계약 준수 실측)
from PIL import Image  # noqa: E402
im = Image.open(SRC).convert("RGB")
WIDTH, HEIGHT = im.size
boxes = [(0, 0, WIDTH // 2, HEIGHT // 2),
         (WIDTH // 2, 0, WIDTH, HEIGHT // 2),
         (0, HEIGHT // 2, WIDTH // 2, HEIGHT),
         (WIDTH // 2, HEIGHT // 2, WIDTH, HEIGHT)]
panel_paths = []
for tag, box in zip(TAGS, boxes):
    p = W.OUT / f"quad_panel_{tag}.png"
    im.crop((box[0] + 4, box[1] + 4, box[2] - 4, box[3] - 4)).save(p)
    panel_paths.append(p)
    print("crop:", p.name)

# ② 사실화만 극대화 i2i (그 외 절대 불변)
REF_LABEL = ("SOURCE FILM STILL — the exact image to refine: every single"
             " element is already final and LOCKED.")
PROMPT = "\n".join([
    "Re-render the attached film still as a maximally PHOTOREALISTIC",
    "live-action photograph — as if the identical frame had been",
    "captured on a real cinema camera on a real physical set.",
    "CHANGE NOTHING except realism: keep the exact same camera framing,",
    "composition, perspective, people, their poses and positions, every",
    "object and its state, the room, all marks and stains, lighting",
    "direction and mood, and time of day PRECISELY as in the source.",
    "Only replace whatever looks rendered, illustrated, waxy or",
    "artificial with true photographic detail: natural human skin and",
    "hair, real fabric and material textures, believable optics (lens",
    "depth of field, natural grain), physically accurate light falloff.",
    "Do NOT beautify, do NOT clean up, do NOT add or remove anything.",
    "No text, captions or watermarks anywhere.",
])
for tag, p in zip(TAGS, panel_paths):
    out = W.OUT / f"quad_refined_{tag}.png"
    F.img_nb2(f"x_quad_refine_{tag}", PROMPT, [(REF_LABEL, p)],
              aspect_ratio="1:1", out_path=out)
    print("refined:", out.name)
print("DONE")
