from __future__ import annotations

from typing import Any


DEFAULT_STYLE_PRESET = "flat_vector_pony"
DEFAULT_QUALITY_PRESET = "pony_high"
DEFAULT_FORMATTER_PROFILE = "manual_controls"

SDXL_STYLE_PRESETS = {
    "flat_vector_pony": "(skindentation:1.25), (flat color:2.0), no lineart, no outline, Flat vector",
    "flat_vector": "(flat color:2.0), no lineart, no outline, Flat vector",
    "photographic": "realistic photo, detailed skin texture, depth of field",
    "none": "",
}

SDXL_QUALITY_PRESETS = {
    "pony_high": (
        "amazing quality, ultra detailed, 8k, very detailed, high detailed texture, "
        "highly detailed anatomy, best quality, newest, very aesthetic, (score_9:1.1), "
        "(score_8_up:1.1), (score_7_up:1.1), masterpiece, absurdres, highres"
    ),
    "sdxl_high": "masterpiece, best quality, amazing quality, ultra detailed, 8k, absurdres, highres",
    "none": "",
}

SDXL_FORMATTER_PROFILES = {
    "manual_controls": {},
    "pony_flat_vector": {
        "style_preset": "flat_vector_pony",
        "quality_preset": "pony_high",
    },
    "sdxl_photo": {
        "style_preset": "photographic",
        "quality_preset": "sdxl_high",
    },
    "flat_vector": {
        "style_preset": "flat_vector",
        "quality_preset": "sdxl_high",
    },
}

SDXL_DEFAULT_NEGATIVE = (
    "worst quality, low quality, normal quality, lowres, bad anatomy, bad hands, "
    "extra fingers, missing fingers, fused fingers, deformed, disfigured, malformed body, "
    "watermark, signature, text, logo, blurry, jpeg artifacts, censored, mosaic censor"
)

SDXL_ACTION_FAMILY_TAGS = {
    "anal": ("anal sex",),
    "foreplay": ("foreplay", "body contact"),
    "manual": ("manual stimulation",),
    "outercourse": ("outercourse", "non-penetrative sex"),
    "oral": ("oral sex",),
    "penetration": ("penetrative sex", "penetration"),
    "threesome": ("threesome",),
    "group": ("group sex",),
    "toy_double": ("double penetration", "toy-assisted sex"),
    "climax": ("climax", "semen"),
}

SDXL_POSITION_FAMILY_TAGS = {
    "penetrative": ("penetrative sex",),
    "foreplay": ("foreplay",),
    "interaction": ("interaction",),
    "manual": ("manual stimulation",),
    "oral": ("oral sex",),
    "outercourse": ("outercourse",),
    "anal": ("anal sex",),
    "climax": ("climax",),
    "threesome": ("threesome",),
    "group": ("group sex",),
}


def sdxl_style_preset_choices() -> list[str]:
    return list(SDXL_STYLE_PRESETS)


def sdxl_quality_preset_choices() -> list[str]:
    return list(SDXL_QUALITY_PRESETS)


def sdxl_formatter_profile_choices() -> list[str]:
    return list(SDXL_FORMATTER_PROFILES)


def _choice_key(value: Any) -> str:
    return str(value or "").strip().lower().replace("-", "_").replace(" ", "_")


def normalize_style_preset(value: str) -> str:
    value = _choice_key(value)
    return value if value in SDXL_STYLE_PRESETS else DEFAULT_STYLE_PRESET


def normalize_quality_preset(value: str) -> str:
    value = _choice_key(value)
    return value if value in SDXL_QUALITY_PRESETS else DEFAULT_QUALITY_PRESET


def normalize_formatter_profile(value: str) -> str:
    value = _choice_key(value)
    return value if value in SDXL_FORMATTER_PROFILES else DEFAULT_FORMATTER_PROFILE


def apply_formatter_profile(
    formatter_profile: str,
    *,
    style_preset: str,
    quality_preset: str,
) -> tuple[str, str]:
    profile = SDXL_FORMATTER_PROFILES[normalize_formatter_profile(formatter_profile)]
    return (
        normalize_style_preset(profile.get("style_preset", style_preset)),
        normalize_quality_preset(profile.get("quality_preset", quality_preset)),
    )
