import os
import json
from pathlib import Path
from shared.openai_manager import run_vision_json  # usamos el shim estable

BASE = Path(__file__).resolve().parents[1]
PROMPT_DETECT = BASE / "prompts" / "detect_type.v1.md"
PROMPT_POS = BASE / "prompts" / "extractor.pos.v1.2.md"
PROMPT_CORP = BASE / "prompts" / "extractor.corporate.v1.2.md"

MODEL_MAP = {
    "mini": os.getenv("OPENAI_MODEL_MINI", "gpt-5-mini"),
    "thinking": os.getenv("OPENAI_MODEL_THINKING", "gpt-5-thinking"),
}


def _load(path: Path) -> str:
    with open(path, "r", encoding="utf-8") as f:
        return f.read()


def detect_type(file_bytes: bytes, filename: str, mimetype: str) -> tuple[str, float]:
    prompt = _load(PROMPT_DETECT)
    print("🟡 detect_type: llamando a run_vision_json con modelo mini")
    res = run_vision_json(
        model=MODEL_MAP["mini"],
        prompt=prompt,
        file_bytes=file_bytes,
        filename=filename,
        mimetype=mimetype,
    )
    print("🟡 detect_type: respuesta recibida")

    if isinstance(res, str):
        res = json.loads(res)

    itype = str(res.get("invoice_type", "")).upper()
    conf = float(res.get("confidence") or 0.0)

    if itype not in ("POS", "CORPORATE"):
        # fallback rápido
        itype = "POS"

    print(f"🟡 detect_type: tipo={itype}, confidence={conf}")
    return itype, conf


def run_extraction(*, data: bytes, filename: str, mimetype: str, engine: str):
    print("🟢 run_extraction: inicio")

    # 1) Detectar tipo
    try:
        itype, conf = detect_type(data, filename, mimetype)
    except Exception as e:
        print(f"🔴 detect_type falló: {e}")
        # fallback duro: asumimos POS
        itype, conf = "POS", 0.0

    # 2) Elegir prompt según tipo
    if itype == "CORPORATE":
        prompt_path = PROMPT_CORP
    else:
        prompt_path = PROMPT_POS

    prompt = _load(prompt_path)
    model = MODEL_MAP.get(engine, MODEL_MAP["mini"])

    print(f"🟢 run_extraction: usando modelo={model}, tipo={itype}, prompt={prompt_path.name}")

    # 3) Llamar extractor principal
    res = run_vision_json(
        model=model,
        prompt=prompt,
        file_bytes=data,
        filename=filename,
        mimetype=mimetype,
    )
    print("🟢 run_extraction: respuesta extracción recibida")

    if isinstance(res, str):
        res = json.loads(res)

    # 4) Ajustar invoice_type de salida
    if isinstance(res.get("invoice_type"), dict):
        existing_conf = float(res["invoice_type"].get("confidence") or 0.0)
        res["invoice_type"]["verbatim"] = itype
        res["invoice_type"]["confidence"] = max(existing_conf, conf)
    else:
        res["invoice_type"] = {"verbatim": itype, "confidence": conf}

    print("🟢 run_extraction: fin OK")
    return res
