from __future__ import annotations

import re
from typing import Any


HARDCORE_ENVIRONMENT_ANCHOR_REPLACEMENTS = (
    (r"\bon against a wall\b", "against a wall"),
    (r"\bstacked bodies on the bed\b", "close body alignment"),
    (r"\bstacked bodies with close body alignment\b", "close body alignment"),
    (r"\boverhead tangled-body anal frame\b", "overhead rear-entry anal frame"),
    (r"\btangled-body\b", "close-body"),
    (r"\bthree bodies tangled on the bed\b", "three bodies tangled in close contact"),
    (r"\ba triangle of bodies on the mattress\b", "a triangle of bodies in close contact"),
    (r"\bbodies tangled on the sheets\b", "bodies tangled in close contact"),
    (r"\bwet bodies tangled on sheets\b", "wet bodies tangled in close contact"),
    (r"\bbody arched on rumpled sheets\b", "body arched with clear skin contact"),
    (r"\bface-down ass-up on the mattress\b", "face-down ass-up position"),
    (r"\bsitting on the edge of the bed\b", "sitting on a raised edge"),
    (r"\blying at the bed edge with thighs open\b", "lying near a raised edge with thighs open"),
    (r"\bedge[- ]of[- ]bed\b", "edge-supported"),
    (r"\bbed[- ]edge\b", "raised edge"),
    (r"\bedge of (?:the )?bed\b", "raised edge"),
    (r"\bbed edge\b", "raised edge"),
    (r"\bhands? braced on the bed\b", "hands braced beside the body"),
    (r"\bone hand pressing into the mattress\b", "one hand braced beside the body"),
    (r"\bone foot planted on the bed\b", "one foot planted for leverage"),
    (r"\bfingers gripping sheets and skin\b", "fingers gripping skin"),
    (r"\bfingers gripping sheets\b", "fingers gripping skin"),
    (r"\bhands gripping sheets\b", "hands gripping skin"),
    (r"\bone hand gripping the sheets\b", "one hand gripping skin"),
    (r"\brumpled bed sheets\b", "wrinkled body-contact fabric"),
    (r"\bwet sheets beneath the bodies\b", "visible wetness beneath the bodies"),
    (r"\bsexual fluids on sheets\b", "sexual fluids visible on skin"),
    (r"\bcum dripping onto sheets\b", "cum visible on skin"),
    (r"\bfluid dripping onto sheets\b", "fluid visible on skin"),
    (r"\bsquirting fluid on the sheets\b", "squirting fluid visible on skin"),
    (r"\bsoft sheets\b", "soft fabric"),
    (r"\bsilk sheets\b", "silk fabric"),
    (r"\bsheets\b", "fabric"),
    (r"\bmattress\b", "low support surface"),
    (r"\ba low support surface\b", "a low body support"),
    (r"\ba low mattress\b", "a low body support"),
    (r"\ba wide couch\b", "a wide body support"),
    (r"\bwide couch\b", "wide body support"),
    (r"\bcouch\b", "body support"),
    (r"\bsofa\b", "body support"),
    (r"\bon the bed\b", "on a body support"),
    (r"\bon a bed\b", "on a body support"),
    (r"\bbedroom-floor\b", "floor-level"),
    (r"\bbedroom floor\b", "floor-level"),
)


def _clean_inline(value: Any) -> str:
    text = "" if value is None else str(value)
    text = text.replace("\n", " ")
    text = re.sub(r"\s+", " ", text).strip()
    text = re.sub(r"\s+([,.;:])", r"\1", text)
    return text


def sanitize_hardcore_environment_anchors(value: Any) -> str:
    text = _clean_inline(value)
    if not text:
        return ""
    for pattern, replacement in HARDCORE_ENVIRONMENT_ANCHOR_REPLACEMENTS:
        text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
    text = re.sub(r"\s+,", ",", text)
    text = re.sub(r",\s*,", ",", text)
    text = re.sub(r"\s{2,}", " ", text)
    return text.strip()


def sanitize_hardcore_axis_value(value: Any) -> Any:
    if isinstance(value, list):
        return [sanitize_hardcore_axis_value(item) for item in value]
    if isinstance(value, dict):
        return {str(key): sanitize_hardcore_axis_value(item) for key, item in value.items()}
    return sanitize_hardcore_environment_anchors(value)


def sanitize_hardcore_axis_values(values: Any) -> dict[str, Any]:
    if not isinstance(values, dict):
        return {}
    return {
        str(key): sanitize_hardcore_axis_value(value)
        for key, value in values.items()
    }
