from __future__ import annotations

import random
from typing import Any

try:
    from .hardcore_role_interaction import build_group_coordination_role_graph, build_manual_role_graph
except ImportError:  # Allows local smoke tests with `python -c`.
    from hardcore_role_interaction import build_group_coordination_role_graph, build_manual_role_graph


def build_support_sentence(rng: random.Random, people: list[str], exclude: set[str]) -> str:
    extras = [person for person in people if person not in exclude]
    if not extras:
        return ""
    extra = rng.choice(extras)
    actions = [
        "kisses and grips the nearest body",
        "holds hips open for the camera",
        "touches breasts, thighs, and stomach",
        "keeps one hand on a partner's ass",
        "watches close and joins the body contact",
        "presses in from the side with hands on skin",
    ]
    return f" {extra} {rng.choice(actions)}."


def build_solo_role_graph(
    solo: str,
    women_count: int,
    slug: str,
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    if women_count == 1:
        if "manual_stimulation" in slug:
            return build_manual_role_graph(solo, item_text=item_text, item_axis_values=item_axis_values)
        if "camera_performance" in slug:
            return f"{solo} faces the camera and presents her body with hands framing the exposed skin in a solo creator-shot pose."
        if "cumshot" in slug or "climax" in slug:
            return f"{solo} is shown in a solo explicit orgasm pose with thighs open, one hand on her body, and visible arousal on skin and sheets."
        return f"{solo} is shown in a solo explicit adult pose with self-touch, open body framing, and direct camera awareness."
    if "cumshot" in slug or "climax" in slug:
        return f"{solo} is shown in a solo visible ejaculation pose with one hand on his penis, body angled toward the camera, and semen visible."
    return f"{solo} is shown in a solo explicit adult pose with direct camera awareness and clear body framing."


def build_women_only_role_graph(
    slug: str,
    a: str,
    b: str,
    c: str = "",
    fallback_helper: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> tuple[str, set[str]]:
    used = {a, b}
    if "manual_stimulation" in slug:
        return build_manual_role_graph(a, b, item_text, item_axis_values), used
    if "group_coordination" in slug and c:
        used.add(c)
        return build_group_coordination_role_graph(a, b, c, item_text=item_text, item_axis_values=item_axis_values), used
    if "outercourse" in slug:
        return f"{a} kneels close to {b}'s body and uses mouth, hands, breasts, or feet for explicit non-penetrative contact.", used
    if "oral" in slug:
        return f"{a} kneels between {b}'s spread thighs and uses tongue and fingers on her pussy.", used
    if "anal" in slug or "double" in slug:
        return f"{a} uses a strap-on on {b} while keeping her hips held open.", used
    if "threesome" in slug or "group" in slug or "orgy" in slug:
        helper = c or fallback_helper or b
        used.add(helper)
        return f"{a} uses a strap-on on {b} while {helper} gives oral contact and touches both bodies.", used
    if "cumshot" in slug or "climax" in slug:
        return f"{a} brings {b} to orgasm with mouth and fingers while wetness is visible on thighs and sheets.", used
    return f"{a} uses a strap-on on {b} while their bodies stay pressed together.", used


def build_men_only_role_graph(
    slug: str,
    a: str,
    b: str,
    c: str = "",
    fallback_helper: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> tuple[str, set[str]]:
    used = {a, b}
    if "manual_stimulation" in slug:
        return f"{a} and {b} sit or recline close together with hands visibly stimulating bodies in a manual sex setup.", used
    if "group_coordination" in slug and c:
        used.add(c)
        return build_group_coordination_role_graph(a, b, c, item_text=item_text, item_axis_values=item_axis_values), used
    if any(token in slug for token in ("foreplay", "body_worship", "clothing_position", "dominant_guidance", "camera_performance", "aftercare")):
        return f"{a} and {b} press close together, kissing and caressing skin while clothing is pulled aside.", used
    if "outercourse" in slug:
        return f"{a} and {b} keep explicit non-penetrative penis contact visible with hands, mouth, or feet.", used
    if "oral" in slug:
        return f"{a} kneels and takes {b}'s penis in his mouth while holding his hips.", used
    if "anal" in slug or "double" in slug or "penetrative" in slug:
        return f"{a} penetrates {b} anally while {b}'s hips are held open.", used
    if "threesome" in slug or "group" in slug or "orgy" in slug:
        helper = c or fallback_helper or b
        used.add(helper)
        return f"{a} penetrates {b} anally while {helper} gives oral contact from the front.", used
    if "cumshot" in slug or "climax" in slug:
        return f"{a} ejaculates semen over {b}'s body while {b} keeps eye contact and one hand on his penis.", used
    return f"{a} and {b} keep explicit penis and anal contact visible.", used


def build_mixed_group_fallback_role_graph(
    woman: str,
    man: str,
    third: str,
    helper: str,
    slug: str,
) -> str:
    if "threesome" in slug:
        return f"{man} thrusts his penis into {woman} while {third or helper} uses mouth and hands on the exposed body."
    if "group" in slug or "orgy" in slug:
        return f"{man} thrusts his penis into {woman} while surrounding partners give oral contact and keep hands on hips, breasts, and thighs."
    return ""
