from __future__ import annotations

import re
from typing import Any

try:
    from . import item_axis_policy
except ImportError:  # Allows local smoke tests with top-level imports.
    import item_axis_policy


def _context_text(item_text: str, item_axis_values: dict[str, Any] | None) -> str:
    return item_axis_policy.context_text(item_text, axis_values=item_axis_values)


def _mentions_ass(text: str) -> bool:
    return bool(
        re.search(
            r"\bass\b|ass[- ](?:up|raised|exposed|lifted)|spread cheeks|lower back and ass|cum (?:on|dripping from) ass|pussy, ass|ass and",
            text,
        )
    )


def build_climax_role_graph(
    woman: str,
    man: str,
    third: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    context = _context_text(item_text, item_axis_values)
    if "lying between two partners" in context and third:
        return f"{woman} lies between {man} and {third}, with {man} under her hips and {third} positioned above her torso as visible semen lands on her body."
    if "held between front-and-back partners" in context and third:
        return f"{woman} is held between {man} behind her and {third} in front of her as visible semen lands across her body."
    if "kneeling between standing partners" in context and third:
        return f"{woman} kneels between {man} and {third} while both stand close around her face and torso for visible ejaculation."
    if "side-lying with thighs parted" in context:
        return f"{woman} lies on her side with thighs parted while {man} kneels beside her hips and ejaculates semen across her thighs and pussy."
    if "sitting on the edge of the bed" in context:
        return f"{woman} sits on the edge of the bed with knees spread while {man} stands close between her legs and ejaculates semen across her body."
    if "lying at the bed edge with thighs open" in context:
        return f"{woman} lies at the bed edge with thighs open while {man} kneels between her legs and ejaculates semen across her pussy and thighs."
    if "reclining with thighs open" in context or "lying on the back with legs spread" in context:
        return (
            f"{woman} lies on her back with thighs open after ejaculation; thick semen and clear fluid cover her exposed pussy "
            f"and inner thighs as the exact-center aftermath detail, her body stays still, and her face and torso remain visible behind the open thighs."
        )
    if "on all fours with hips raised" in context:
        return f"{woman} is on all fours with hips raised while {man} is positioned behind her and ejaculates semen across her ass, thighs, and lower back."
    if "face-down ass-up" in context:
        return f"{woman} lies face-down with ass raised while {man} is positioned behind her and ejaculates semen across her lower back and ass."
    if "bent over with ass raised" in context or "bent over" in context:
        return f"{woman} is bent forward with hips raised while {man} is positioned behind her, visible semen across her lower back, ass, and thighs."
    if "kneeling with mouth open" in context:
        return f"{woman} kneels in front of {man} at hip height while {man} ejaculates semen onto her face, lips, and chest."
    if "kneeling in front of a standing partner" in context:
        return f"{woman} kneels in front of {man} at hip height while {man} stands over her and ejaculates semen across her body."
    if "standing with cum on the body" in context:
        return f"{woman} stands braced in front of {man} while he stays close at hip level and ejaculates semen across her body."
    if "squatting on top of a partner" in context:
        return f"{woman} squats over {man}'s hips while {man} lies on his back under her and ejaculates semen onto her body."
    if "reverse cowgirl over a partner's hips" in context:
        return f"{woman} straddles {man}'s hips facing away while {man} lies on his back under her and ejaculates semen onto her body."
    if any(term in context for term in ("straddling a partner", "straddling a partner's hips", "shared climax after penetration", "orgasm during penetration")):
        return f"{woman} straddles {man}'s hips while {man} lies on his back under her, their bodies still aligned from penetration as he ejaculates semen onto her body."
    if "seated in a partner's lap facing them" in context:
        return f"{woman} sits in {man}'s lap facing him, legs wrapped around his hips as he ejaculates semen across her body."
    if any(term in context for term in ("lower back", "cum dripping from ass", "cum on lower back")) or _mentions_ass(context):
        return f"{woman} is bent forward with hips raised while {man} is positioned behind her, visible semen across her lower back, ass, and thighs."
    if any(term in context for term in ("cum on face", "cum on tongue", "cum on lips", "cum on face and lips", "cum on tongue and chin")):
        if third:
            return f"{woman} kneels in the center while {man} and {third} stand close around her face and torso for visible ejaculation."
        return f"{woman} kneels in front of {man} at hip height while {man} ejaculates semen onto her face, lips, and chest."
    return f"{woman} lies on her back with thighs open while {man} kneels between her legs and ejaculates semen onto her body."
