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 : véhicule/personnage EN PREMIER (Image 0), décors ensuite.
# Le Bulle est le "personnage" principal — sa cohérence visuelle est critique.
# ─────────────────────────────────────────────────────────────────────────────
REFS = {
    # --- VÉHICULE PRINCIPAL (Image 0) ---
    "submarine":   "sous-marin.png",   # Image 0 -> Le Bulle (vehicle/character)
    # --- DÉCORS (Images 1 à 3) ---
    "coraux":      "coraux.png",       # Image 1 -> Le Récif de Corail (coral reef)
    "starfish":    "starfish.png",     # Image 2 -> Le Fond de l'Océan (ocean floor)
    "underwater":  "underwater.png",   # Image 3 -> L'Océan Profond (deep blue water)
}

CHARACTER_MAPPING = (
    "STRICT IDENTITY MAPPING — follow exactly: "
    "Vehicle 1 (LE BULLE SUBMARINE) = Image 0. "
    "Background A (LE RECIF DE CORAIL) = Image 1. "
    "Background B (LE FOND DE L'OCEAN) = Image 2. "
    "Background C (L'OCEAN PROFOND) = Image 3. "
)

LOCK_PHRASES = (
    "Maintain strict visual consistency of Le Bulle submarine as shown in the reference image. "
    "Ensure Le Bulle's round yellow hull, teal-blue lower half, large glass bubble dome cockpit, "
    "round headlights, teal propeller, 'Le Bulle' lettering, and overall design remain 100% identical "
    "to the reference sheet in every scene. Do not alter Le Bulle's shape, color, or proportions. "
)

IMAGE_DESCRIPTIONS = (
    "Image 0 = LE BULLE SUBMARINE: round chubby yellow metal hull with teal-blue lower half, "
    "large transparent glass bubble dome cockpit at front with yellow leather seat and colorful "
    "control panels visible inside, 'Le Bulle' written in teal letters on the side, round yellow "
    "headlights on front, teal propeller at rear, small round portholes on sides, blue periscope "
    "on top, friendly charming design. Same design every scene — front view, 3/4 view, side profile all shown. "
    "Image 1 = LE RECIF DE CORAIL: vibrant magical underwater coral reef, towering colorful coral "
    "formations in pink purple orange and yellow, schools of tropical fish in every color — clownfish, "
    "angelfish, yellow tangs — clear bright turquoise water, caustic light rays from the surface, "
    "rich biodiversity. Use for pages 3-6. "
    "Image 2 = LE FOND DE L'OCEAN: the sandy ocean floor, warm amber bioluminescent light from above, "
    "colorful starfish and purple sea urchins in foreground, spiral shells, green sea grass, "
    "rippled golden sand, warm magical underwater atmosphere. Use for page 12. "
    "Image 3 = L'OCEAN PROFOND: deep open ocean with dark navy blue gradients fading to near black, "
    "thin light rays from the distant surface above, vast empty depth and scale, mysterious and beautiful. "
    "Use for pages 7-11 and descent/ascent scenes. "
)

PRENOM = "Leo"


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_ocean"):
    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}...")

    text = (
        CHARACTER_MAPPING
        + LOCK_PHRASES
        + IMAGE_DESCRIPTIONS
        + f"CHILD CHARACTER {PRENOM}: short slightly wavy brown hair, big round brown eyes, rosy cheeks, "
          f"blue denim overalls over a yellow t-shirt, white sneakers. Same face and outfit every scene. "
          f"Visible through Le Bulle's glass dome in underwater scenes, on the beach in pages 1, 14, 15, 16. "
        + "PARENT CHARACTER (pages 15-16 only): sage green knitted sweater, warm kind eyes, loving expression. "
        + "Now generate: " + full_prompt
    )

    # Le Bulle en premier (Image 0), décors ensuite (Images 1→3)
    parts = [types.Part(text=text)]
    for key in ["submarine", "coraux", "starfish", "underwater"]:
        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_ocean.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_ocean_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_ocean.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)
