"""인물 + 변형 나열 — gemini flash lite 테스트."""

import json, sys
sys.path.insert(0, ".")

from app.modules.llm.llm_client import call_structured
from app.core.config import settings

PID = "c0f47e88-b9f7-43a5-9e73-66b0f965b247"
EID = "d2d203b8-4865-40f0-b5a6-e5990fa3effd"
CP = f"{settings.projects_dir}/{PID}/checkpoints/episodes/{EID}"

seg = json.load(open(f"{CP}/scene_save/manifest.json"))

from app.core.database import SessionLocal
from app.models.project import Episode
from sqlalchemy.orm import undefer
db = SessionLocal()
ep = db.query(Episode).options(undefer(Episode.fulltext)).filter(Episode.id == EID).first()
ft = ep.fulltext
db.close()

parts = []
for s in seg["data"]["segments"]:
    parts.append(f"[씬 {s['scene_index']}]\n{s.get('text', '')}")
scene_block = "\n\n".join(parts)
print(f"입력: {len(seg['data']['segments'])}씬, {len(scene_block)}자")

schema = {
    "type": "object",
    "properties": {
        "characters": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "variants": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "변형 형태 목록 (변신, 요기 해방, 동물화 등). 없으면 빈 배열"
                    }
                },
                "required": ["name", "variants"],
                "additionalProperties": False
            }
        }
    },
    "required": ["characters"],
    "additionalProperties": False
}

result = call_structured(
    step="text_cleanup",
    system_prompt="시나리오에서 등장하는 인물과 그 변형(변신/변형/외형 변화) 형태를 나열하세요.",
    user_prompt=scene_block,
    response_schema=schema,
    schema_name="char_variants",
)

print(f"\n인물: {len(result['characters'])}명")
for c in result["characters"]:
    v = ", ".join(c["variants"]) if c["variants"] else "-"
    print(f"  {c['name']:20s} → {v}")
