"""
Combines the local-CV technical score with Gemini's yes/no answers into the
four weighted dimensions from spec section 8/9, using a scoring table we
control (not numbers the model invents). Every (true_score, false_score) pair
below is a first-pass guess - the intent is that these get tuned later once
we have "AI recommendation vs actual user decision" data (open question #1),
not that they're correct today.
"""

WEIGHTS = {
    "technical": 0.40,
    "subject": 0.20,
    "composition": 0.25,
    "emotion": 0.15,
}


def _bool_avg(pairs: list[tuple[bool, float, float]]) -> float:
    scores = [true_score if value else false_score for value, true_score, false_score in pairs]
    return sum(scores) / len(scores)


def score_subject(subject: dict) -> float:
    return _bool_avg([
        (subject["subject_clear_at_first_glance"], 100, 40),
        (subject["eyes_open"], 100, 30),
        (subject["expression_natural"], 100, 50),
    ])


def score_composition(composition: dict) -> float:
    return _bool_avg([
        (composition["follows_thirds_or_intentional_framing"], 100, 50),
        (not composition["background_distracting"], 100, 40),
        (composition["subject_well_positioned"], 100, 40),
    ])


def score_emotion(emotion: dict) -> float:
    return _bool_avg([
        (emotion["stranger_would_pause"], 100, 30),
        (emotion["captures_a_moment"], 100, 40),
    ])


def recommendation_from_score(overall_score: float) -> str:
    if overall_score >= 75:
        return "keep"
    if overall_score >= 50:
        return "review"
    return "reject"


def combine(technical_score: float, ai_result: dict) -> dict:
    subject_score = round(score_subject(ai_result["subject"]), 1)
    composition_score = round(score_composition(ai_result["composition"]), 1)
    emotion_score = round(score_emotion(ai_result["emotion_story"]), 1)

    overall_score = round(
        technical_score * WEIGHTS["technical"]
        + subject_score * WEIGHTS["subject"]
        + composition_score * WEIGHTS["composition"]
        + emotion_score * WEIGHTS["emotion"],
        1,
    )

    return {
        "technical_score": technical_score,
        "subject_score": subject_score,
        "composition_score": composition_score,
        "emotion_score": emotion_score,
        "overall_score": overall_score,
        "recommendation": recommendation_from_score(overall_score),
    }
