import os
import json
import time
import random
from pathlib import Path
from google import genai
from google.genai import types

GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
client = genai.Client(api_key=GEMINI_API_KEY)

# ─────────────────────────────────────────────────────────────────────────────
# ORDRE CRITIQUE : personnages en premier (Image 0, 1, 2), decors ensuite.
# L'IA donne le plus de poids aux premieres images pour la coherence des visages.
# ─────────────────────────────────────────────────────────────────────────────
REFS = {
    # --- PERSONNAGES (Images 0, 1, 2) ---
    "princesse":    "princesse.png",       # Image 0 -> Character 1
    "prince":       "prince.png",          # Image 1 -> Character 2
    "dragon":       "Dragon.png",          # Image 2 -> Character 3
    # --- DECORS (Images 3 a 8) ---
    "castle_day":   "Chateau_jour.png",    # Image 3 -> Background A (matin)
    "castle_plain": "Chateau_plaine.png",  # Image 4 -> Background B (plaine)
    "castle_night": "Chateau_soir.png",    # Image 5 -> Background C (soir)
    "cliff":        "Cliff.png",           # Image 6 -> Background D (falaise)
    "cloud_island": "cloud_island.png",    # Image 7 -> Background E (plateau nuages)
    "crystal_cave": "crystal_cave.png",    # Image 8 -> Background F (grotte)
}

# Mapping explicite injecte dans chaque appel API
CHARACTER_MAPPING = (
    "STRICT IDENTITY MAPPING — follow exactly: "
    "Character 1 (PRINCESS {prenom}) = Image 0. "
    "Character 2 (THE PRINCE) = Image 1. "
    "Character 3 (THE DRAGON) = Image 2. "
    "Background references = Images 3 to 8. "
)

# Lock phrases (verrouillage de coherence)
LOCK_PHRASES = (
    "Maintain strict facial likeness of every character as shown in their reference image. "
    "Ensure character structure, clothing colors, and hair style remain 100% identical to the reference sheet. "
    "Do not alter character proportions or anatomy. "
)

PRENOM = "Sofia"


def load_references():
    loaded = {}
    for name, path in REFS.items():
        if Path(path).exists():
            loaded[name] = Path(path).read_bytes()
            print(f"Reference chargee : {path}")
        else:
            loaded[name] = None
            print(f"Pas de reference : {path}")
    return loaded


def call_with_retry(parts, max_retries=5):
    for attempt in range(max_retries):
        try:
            return client.models.generate_content(
                model="gemini-3-pro-image-preview",
                contents=parts,
                config=types.GenerateContentConfig(
                    response_modalities=["IMAGE", "TEXT"]
                )
            )
        except Exception as e:
            wait = (2 ** attempt) + random.uniform(0, 2)
            print(f"Tentative {attempt + 1}/{max_retries} echouee : {e}")
            if attempt < max_retries - 1:
                print(f"Attente {wait:.1f}s avant retry...")
                time.sleep(wait)
            else:
                raise


def generate_page(page_data, style_master, refs, output_dir="pages_chateau"):
    Path(output_dir).mkdir(exist_ok=True)
    page_num = page_data["page"]
    page_type = page_data["type"]
    prompt = page_data["prompt"].replace("{prenom}", PRENOM)
    output_path = f"{output_dir}/page_{page_num:02d}.png"

    format_instruction = (
        "IMPORTANT: single full square illustration 2362x2362 pixels, no spread, no border, fills entire frame."
        if page_type == "simple"
        else "IMPORTANT: double page spread 4724x2362 pixels, 2:1 ratio, safe center fold, no spine line, fills entire frame."
    )

    full_prompt = style_master + " " + format_instruction + " Scene: " + prompt

    print(f"Page {page_num:02d}...")

    # ─────────────────────────────────────────────────────────────────────────
    # TEXTE : mapping explicite + lock phrases + descriptions + prompt
    # ─────────────────────────────────────────────────────────────────────────
    character_mapping = CHARACTER_MAPPING.replace("{prenom}", PRENOM)

    text = (
        character_mapping
        + LOCK_PHRASES
        + "Reference descriptions for extra clarity — "
        + f"Image 0 = PRINCESS {PRENOM}: 5-6 year old girl, long flowing brown wavy hair, big round brown eyes, rosy cheeks, lavender-pink ball gown with gold embroidered trim, small gold tiara with pink gemstone, pink shoes. "
        + "Image 1 = THE PRINCE: young boy, short neat brown hair, big round brown eyes, rosy cheeks, blue sleeveless vest with gold trim over cream long-sleeve shirt, grey pants, brown leather boots, small brown belt. "
        + "Image 2 = THE DRAGON: chubby round friendly dragon, teal-blue smooth rounded scales, lime-green lighter belly, big round warm green eyes, small bat-like wings, two small beige curved horns, small dorsal spines — NOT scary, NOT aggressive. "
        + "Image 3 = CHATEAU JOUR: pastel castle, terracotta domed towers, green hills, warm sunrise. "
        + "Image 4 = CHATEAU PLAINE: same castle seen from open meadow, rolling hills, golden light. "
        + "Image 5 = CHATEAU SOIR: evening castle, lantern light, purple-pink twilight sky, mist. "
        + "Image 6 = LA FALAISE DU DRAGON: dramatic rocky mountain cliff plateau, snow-capped peaks, orange-gold sunset sky. "
        + "Image 7 = LE PLATEAU DES JEUX: magical floating cloud island, colorful wildflowers, crystal arch, purple-golden sky above clouds. "
        + "Image 8 = L'INTERIEUR DE LA GROTTE: magical crystal cave, glowing amethyst-purple and blue crystals, stone path, small stream, hanging lanterns. "
        + "Now generate: " + full_prompt
    )

    # ─────────────────────────────────────────────────────────────────────────
    # ORDRE DES PARTS : texte, puis personnages (0->1->2), puis decors (3->8)
    # ─────────────────────────────────────────────────────────────────────────
    parts = [types.Part(text=text)]
    for key in ["princesse", "prince", "dragon",
                "castle_day", "castle_plain", "castle_night",
                "cliff", "cloud_island", "crystal_cave"]:
        if refs.get(key):
            parts.append(types.Part(
                inline_data=types.Blob(data=refs[key], mime_type="image/png")
            ))

    response = call_with_retry(parts)

    for part in response.candidates[0].content.parts:
        if part.inline_data:
            with open(output_path, "wb") as f:
                f.write(part.inline_data.data)
            print(f"OK : {output_path}")
            return output_path

    print(f"ERREUR page {page_num} : aucune image dans la reponse")
    return None


def generate_book(prompts_file="prompts_chateau.json", start_page=1, end_page=None):
    with open(prompts_file, "r", encoding="utf-8") as f:
        data = json.load(f)

    style_master = data["style_master"]
    pages = data["pages"]

    if end_page:
        pages = [p for p in pages if start_page <= p["page"] <= end_page]
    else:
        pages = [p for p in pages if p["page"] >= start_page]

    refs = load_references()

    print(f"Generation : {len(pages)} pages — prenom : {PRENOM}")
    results = []

    for page_data in pages:
        path = generate_page(page_data, style_master, refs)
        results.append({
            "page": page_data["page"],
            "path": path,
            "success": path is not None
        })
        time.sleep(5)

    success = sum(1 for r in results if r["success"])
    print(f"\nTermine : {success}/{len(pages)} pages generees")

    with open("book_chateau_results.json", "w") as f:
        json.dump(results, f, indent=2)

    return results


if __name__ == "__main__":
    import sys
    prompts_file = sys.argv[1] if len(sys.argv) > 1 else "prompts_chateau.json"
    start = int(sys.argv[2]) if len(sys.argv) > 2 else 1
    end = int(sys.argv[3]) if len(sys.argv) > 3 else None
    generate_book(prompts_file, start, end)
