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 _anal_position_graph(woman: str, man: str, context: str) -> str:
    if "bent-over" in context or "bent over" in context:
        return f"{woman} is bent forward with hips raised while {man} stands behind her and thrusts his penis into her ass."
    if "face-down" in context:
        return f"{woman} lies face-down with ass raised while {man} is positioned behind her and thrusts his penis into her ass."
    if "doggy" in context or "rear-entry" in context:
        return f"{woman} is on all fours with hips raised while {man} is positioned behind her and thrusts his penis into her ass."
    if "standing" in context:
        return f"{woman} stands braced with hips angled back while {man} stands behind her and thrusts his penis into her ass."
    if "spooning" in context or "side-lying" in context:
        return f"{woman} lies on her side with thighs parted while {man} presses behind her and thrusts his penis into her ass."
    if "edge-of-bed" in context or "edge of bed" in context or "bed edge" in context or "edge-supported" in context:
        return f"{woman} lies near a raised edge with hips exposed while {man} kneels behind her and thrusts his penis into her ass."
    if "kneeling" in context:
        return f"{woman} kneels forward with hips raised while {man} kneels behind her and thrusts his penis into her ass."
    return f"{woman} is on all fours with hips raised while {man} is positioned behind her and thrusts his penis into her ass."


def _two_person_double_graph(woman: str, man: str, context: str) -> str:
    if "bent-over" in context or "bent over" in context:
        return f"{woman} is bent forward with hips raised while {man} is positioned behind her and thrusts his penis into her ass."
    if "face-down" in context:
        return f"{woman} lies face-down with hips raised while {man} is positioned behind her and thrusts his penis into her ass."
    if "standing" in context:
        return f"{woman} stands braced with hips raised while {man} is positioned behind her and thrusts his penis into her ass."
    if "kneeling" in context:
        return f"{woman} kneels forward with hips raised while {man} is positioned behind her and thrusts his penis into her ass."
    return f"{woman} is on all fours with hips raised while {man} is positioned behind her and thrusts his penis into her ass."


def build_anal_or_double_role_graph(
    woman: str,
    man: str,
    third: str,
    people_count: int,
    item_text: str,
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    context = _context_text(item_text, item_axis_values)
    if "double" in context or "toy" in context:
        if people_count >= 3 and third:
            return f"{man} thrusts his penis into {woman} while {third} adds a second penetration point from the front."
        return _two_person_double_graph(woman, man, context)
    if people_count >= 3 and third:
        return f"{man} thrusts his penis into {woman} while {third} gives oral contact from the front."
    return _anal_position_graph(woman, man, context)
