from __future__ import annotations

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 build_penetration_role_graph(
    woman: str,
    man: str,
    item_text: str,
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    text = _context_text(item_text, item_axis_values)
    if "folded missionary" in text or "knees-to-chest" in text or "knees to chest" in text:
        return (
            f"{woman} lies on her back facing {man} with knees folded high toward her chest while {man} is above her between her thighs; "
            f"{man}'s hands hold her calves and {man}'s penis thrusts into her pussy below the raised knees."
        )
    if "missionary" in text:
        return (
            f"{woman} lies on her back with legs open around {man}'s hips while {man} is above her between her thighs; "
            f"{man}'s hips press close and {man}'s penis thrusts into her pussy."
        )
    if "cowgirl-alt" in text or "low cowgirl" in text or "seated-squat cowgirl" in text or "low seated squat" in text:
        return (
            f"{woman} faces {man} in a low seated squat over {man}'s pelvis while {man} lies flat on his back under her; "
            f"{man} supports the underside of her thighs and {man}'s penis thrusts into her pussy."
        )
    if "reverse cowgirl alt" in text or "upright reverse cowgirl" in text or "upright back-facing straddle" in text:
        return (
            f"{woman} sits upright facing away in a back-facing straddle over {man}'s pelvis while {man} lies under her; "
            f"{man}'s hands hold her hips and {man}'s penis thrusts into her pussy."
        )
    if "reverse cowgirl" in text:
        return f"{woman} straddles {man}'s hips facing away while {man} lies under her and {man}'s penis thrusts into her pussy."
    if "cowgirl" in text or "straddling" in text:
        return (
            f"{woman} straddles {man}'s hips facing him while {man} lies under her; "
            f"{man}'s lower abdomen and pelvis anchor the bottom edge, {woman}'s thighs form a wide horizontal thigh bridge from left edge to right edge, "
            f"her knees plant outside {man}'s hips, {man}'s hands grip the sides of her thighs, and centered contact remains below her belly as {man}'s penis thrusts into her pussy."
        )
    if "doggy" in text or "rear-entry" in text or "bent-over" in text or "bent over" in text:
        return f"{woman} is on all fours with hips raised while {man} is positioned behind her and {man}'s penis thrusts into her pussy."
    if "standing" in text:
        return f"{woman} stands braced with hips angled back while {man} stands behind her and {man}'s penis thrusts into her pussy."
    if "spooning" in text or "side-lying" in text:
        return f"{woman} lies on her side with thighs parted while {man} presses behind her and {man}'s penis thrusts into her pussy."
    if "edge-of-bed" in text or "edge of bed" in text or "bed edge" in text or "edge-supported" in text or "raised edge" in text:
        return (
            f"{woman} lies back at a raised edge with hips at the edge and legs open while {man} kneels between her thighs; "
            f"{man}'s hips press close and {man}'s penis thrusts into her pussy."
        )
    if "kneeling straddle" in text:
        return f"{woman} kneels straddling {man}'s hips while {man} supports her waist and {man}'s penis thrusts into her pussy."
    if "lotus" in text:
        return f"{woman} sits in {man}'s lap facing him with legs around his hips while {man}'s penis thrusts into her pussy."
    return (
        f"{woman} lies on her back with legs spread wide and knees bent outward while {man} kneels between her open thighs facing her; "
        f"{man}'s hips are pressed between her legs and {man}'s penis thrusts into her pussy."
    )
