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_foreplay_role_graph(
    primary: str,
    partner: str,
    item_text: str,
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    text = _context_text(item_text, item_axis_values)
    if any(term in text for term in ("undressing", "removing clothing", "removing clothes", "pulling clothing", "sliding straps", "unbuttoning")):
        return (
            f"{primary} and {partner} stand close while {partner}'s hands pull clothing aside from {primary}'s body; "
            f"{primary}'s exposed skin and the clothing being removed stay clearly visible."
        )
    if any(term in text for term in ("breast", "breasts", "nipple", "cupping breasts", "touching breasts")):
        return (
            f"{primary} and {partner} press their bodies close while {partner}'s hand cups {primary}'s breast; "
            f"their faces stay close and the breast-touching gesture is clear."
        )
    if any(term in text for term in ("face", "cheek", "jaw", "chin", "hand on the cheek", "fingers under the chin")):
        return (
            f"{primary} and {partner} stand face-to-face at close range while one hand holds {primary}'s cheek and jaw; "
            f"their lips are close and the face-touching gesture is clear."
        )
    if any(term in text for term in ("kiss", "kissing", "mouth-to-mouth", "lips pressed")):
        return (
            f"{primary} and {partner} press their bodies together and kiss deeply, "
            f"with hands on each other's face, waist, and hips."
        )
    return (
        f"{primary} and {partner} are pressed close in a heated foreplay setup, "
        f"hands caressing skin while clothing is pulled aside."
    )


def build_manual_role_graph(
    primary: str,
    partner: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    text = _context_text(item_text, item_axis_values)
    if not partner:
        if "mutual" in text:
            return f"{primary} faces the camera with thighs open, both hands on her body for solo mutual-style masturbation framing."
        return f"{primary} reclines with thighs open, one hand between her legs and fingers visibly stimulating her pussy."
    if "mutual" in text:
        return f"{primary} and {partner} sit close facing each other, both touching themselves while keeping hands, faces, and bodies visible."
    if "toy" in text or "vibrator" in text or "wand" in text:
        return (
            f"{primary} reclines in a close first-person toy-contact view with thighs spread wide toward the camera; "
            "a single continuous teal wand-style massager is the largest lower-frame object, "
            "the rounded bulb head presses flat to her vulva and clit as the central contact point, "
            f"and the smooth handle angles in from the bottom right inside {partner}'s visible hand. "
            f"{primary}'s open thighs and knees form a V around the foreground wand while her face and torso remain visible behind the leg frame."
        )
    if "clit" in text or "clitoris" in text:
        return (
            f"{primary} reclines with thighs open while {partner}'s foreground hand is the largest lower-frame object; "
            f"her open thighs form a V around the hand, the wrist enters from the bottom center, "
            f"and two fingers press at her vulva and clit as the clear contact point while her face and torso remain visible behind the open thighs."
        )
    return (
        f"{primary} reclines with thighs open while {partner}'s foreground hand is the largest lower-frame object; "
        f"her open thighs form a V around the hand, the wrist enters from the bottom center, "
        f"and two fingers at her vulva and clit make the central manual-contact point while her face and torso remain visible behind the open thighs."
    )


def build_interaction_role_graph(
    primary: str,
    partner: str,
    third: str = "",
    slug: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    text = _context_text(item_text, item_axis_values)
    if "aftercare" in slug or any(term in text for term in ("aftercare", "cleanup", "wiping", "towel", "post-sex", "cuddle")):
        if "cleanup" in text or "wiping" in text or "towel" in text:
            return f"{primary} reclines after sex while {partner} kneels close and wipes her skin with a towel, hands and relaxed body contact visible."
        return f"{primary} and {partner} lie close together after sex, bodies relaxed and hands resting on skin in a post-sex cuddle."
    if "camera_performance" in slug or any(term in text for term in ("camera", "presenting", "showing", "viewer", "creator-shot")):
        if any(term in text for term in ("spread open", "open thighs", "thighs open", "legs spread", "knees held wide")):
            return (
                f"{primary} sits back facing the camera with knees raised and held wide; "
                f"her thighs form a broad V-frame around her centered exposed vulva, "
                f"her hands hold her knees and upper thighs, and her face and torso remain visible behind the open-thigh frame."
            )
        if third:
            return f"{primary} faces the camera while {partner} and {third} hold and present her body, hands framing the exposed skin for the viewer."
        return f"{primary} faces the camera and presents her body while {partner}'s hands hold her hips or thighs open for a clear creator-shot reveal."
    if "body_worship" in slug or any(term in text for term in ("body worship", "nipple", "thigh", "mouth on skin", "kissing down", "ass grabbing")):
        if "ass" in text:
            return f"{primary} stands or kneels with hips angled back while {partner}'s hands grip her ass, fingers pressing into skin."
        if "thigh" in text:
            return f"{primary} reclines with thighs open while {partner} kneels close and kisses along her inner thighs, hands holding her legs in place."
        if "nipple" in text or "breast" in text:
            return f"{primary} arches toward {partner} while {partner}'s mouth is on her breast and one hand cups or squeezes the other breast."
        return f"{primary} reclines or leans back while {partner} kisses down her body, hands tracing breasts, waist, hips, and thighs."
    if "clothing_position" in slug or any(term in text for term in ("transition", "turning", "pulling onto", "lifting", "guided backward", "clothing", "garment")):
        if "turn" in text or "rear-facing" in text:
            return f"{partner}'s hands turn {primary} around by the hips, clothing partly moved aside as her body rotates into the next pose."
        if "legs" in text or "thigh" in text:
            return f"{primary} lies back while {partner} lifts and spreads her legs into position, hands and clothing movement clearly visible."
        return f"{primary} and {partner} are mid-transition, with {partner}'s hands moving clothing aside and guiding {primary}'s hips toward the next pose."
    if "dominant" in slug or any(term in text for term in ("hair", "wrist", "wrists", "jaw", "chin", "guided", "dominant", "control", "dirty talk", "whisper", "mouth near the ear", "verbal teasing")):
        if "dirty talk" in text or "whisper" in text or "mouth near the ear" in text or "verbal teasing" in text:
            return f"{partner} leans close to {primary}'s ear for dirty talk while holding her waist and keeping their bodies pressed close."
        if "wrist" in text or "wrists" in text:
            return f"{primary} lies back while {partner} pins her wrists above her head, both bodies close and the consensual control gesture clearly visible."
        if "hair" in text:
            return f"{partner} holds {primary}'s hair back while guiding her body closer, face and hair-hold gesture visible."
        if "thigh" in text or "spread" in text:
            return f"{primary} reclines with thighs open while {partner}'s hands spread her legs and hold the position for the camera."
        return f"{partner} guides {primary}'s body with hands on her jaw, waist, and hips, keeping the consensual control gesture readable."
    return build_foreplay_role_graph(primary, partner, item_text, item_axis_values)


def build_group_coordination_role_graph(
    primary: str,
    partner: str,
    third: str = "",
    fallback_observer: str = "",
    item_text: str = "",
    item_axis_values: dict[str, Any] | None = None,
) -> str:
    observer = third or fallback_observer or partner
    text = _context_text(item_text, item_axis_values)
    if "camera" in text or "hold" in text or "present" in text:
        return f"{primary} is centered while {partner} and {observer} hold and present the body for the camera, each role clearly visible."
    if "watch" in text or "waiting" in text:
        return f"{primary} is centered while {partner} touches her body and {observer} watches close beside them, hands and faces readable."
    return f"{primary} is centered while {partner} touches her body and {observer} stays close as the watching or guiding partner."
