import sys, random
sys.stdout.reconfigure(encoding='utf-8')
from pathlib import Path

ROOT_DIR = Path(__file__).resolve().parent.parent
sys.path.append(str(ROOT_DIR))

from Engine.prompt_builder import PromptBuilder
from Engine.api_client import ForgeClient

pbuilder = PromptBuilder(ROOT_DIR)
forge = ForgeClient()

tests = [
    {
        'id': '19153186',
        'char_name': 'Columbina',
        'char_line': '<lora:columbina_gnsn:0.8> columbina, genshin impact, blue eyes, pink hair, two-tone hair, long hair, halo, wings',
        'level': 'explicito',
        'pose_tags': 'indoors, luxurious bedroom, dark velvet bed, on_bed, on_stomach, ass_up, blindfold, feet_up, looking_at_viewer, erect_nipples, seductive, from side'
    },
    {
        'id': '19227077',
        'char_name': 'Rias_Gremory',
        'char_line': 'rias gremory, high school dxd, blue eyes, red hair, very long hair, ahoge, huge ahoge, large breasts',
        'level': 'explicito',
        'pose_tags': 'indoors, private chamber, ornate headboard, lying_on_back, missionary, from_above, arms_behind_head, choker, navel, thick_thighs'
    },
    {
        'id': '19228658',
        'char_name': 'Shouko_Komi',
        'char_line': '<lora:Shouko_Komi_Komi_Cant_Communicate_-_Illustrious_V2:0.8> sh0uk0, 1girl, solo, dark purple hair, long hair, purple eyes, medium breasts',
        'level': 'explicito',
        'pose_tags': 'indoors, quiet library, reading desk, on_side, spread_legs, leg_lift, breast_grab, seductive'
    },
    {
        'id': '19227655',
        'char_name': 'Tatsumaki',
        'char_line': '<lora:Tatsumaki_OnePunchMan_Ilxl_V1:0.8> ttsumk, green hair, short hair, curl, green eyes, young adult',
        'level': 'sugerente',
        'pose_tags': 'indoors, modern apartment, minimalist interior, clothed, tight_clothes, curvy, solo_female'
    },
    {
        'id': '19227044',
        'char_name': 'Aqua',
        'char_line': '<lora:Aqua_KonoSuba_Ilxl_V1:0.8> aquaknsb, blue hair, long hair, blue eyes, water halo, ribbon',
        'level': 'spicy',
        'pose_tags': 'indoors, celestial shrine, marble floor, kneeling, bare_shoulders, seductive'
    },
    {
        'id': '19226958',
        'char_name': 'Nicole_Demara',
        'char_line': '<lora:Hoseki_ZenlessZoneZero_NicoleDemara_IllustriousXL_v1:0.8> n1c0le, large breasts, green eyes, pink hair, long hair, two side up',
        'level': 'spicy',
        'pose_tags': 'outdoors, urban alleyway, neon lights, brick wall, leotard, fishnet_pantyhose, spread_legs, three_quarter_view'
    }
]

out_root = ROOT_DIR / 'Output' / 'candidate_tests'
out_root.mkdir(parents=True, exist_ok=True)

for t in tests:
    print(f"\n--- PROBANDO CANDIDATA ID {t['id']} ({t['char_name']}) ---")
    master_part = pbuilder.master_prompt
    char_tramo = f"1girl, {t['char_line']}"
    
    tramos = [master_part, char_tramo, t['pose_tags']]
    full_prompt = "\nBREAK\n".join(tramos)
    neg_prompt = pbuilder.negative_prompt
    
    print("Prompt:", full_prompt[:120] + "...")
    
    try:
        images, info = forge.txt2img(
            prompt=full_prompt,
            negative_prompt=neg_prompt,
            width=832,
            height=1216,
            steps=28,
            cfg_scale=6.0,
            sampler_name="DPM++ 2M Karras",
            seed=42,
            clip_skip=2
        )
        if images:
            file_path = out_root / f"{t['id']}_{t['char_name']}.png"
            file_path.write_bytes(images[0])
            print(f"✓ Generado y guardado en: {file_path}")
    except Exception as e:
        print(f"❌ Error generando candidata {t['id']}: {e}")
