"""
Deterministic verb calibration for video prompt engineering.

Maps aggressive/dramatic narrative verbs to physics-grounded equivalents
that Kling (and other video models) interpret reliably. Applied BEFORE
Flash/Sonnet/Opus enrichment so the enrichment model never sees uncalibrated verbs.

Usage:
    from recoil.pipeline._lib.verb_calibration import calibrate_verbs
    prompt = calibrate_verbs(raw_prompt)
"""

import re

VERB_CALIBRATION: dict[str, str] = {
    # --- Impact ---
    "slams": "presses firmly",
    "slam": "press firmly",
    "slamming": "pressing firmly",
    "slammed": "pressed firmly",
    "smashes": "strikes",
    "smash": "strike",
    "smashing": "striking",
    "smashed": "struck",
    "crashes": "collides",
    "crash": "collide",
    "crashing": "colliding",
    "crashed": "collided",
    "pounds": "knocks",
    "pound": "knock",
    "pounding": "knocking",
    "pounded": "knocked",

    # --- Explosive movement ---
    "explodes": "moves quickly",
    "explode": "move quickly",
    "exploding": "moving quickly",
    "exploded": "moved quickly",
    "rockets": "accelerates",
    "rocket": "accelerate",
    "rocketing": "accelerating",
    "rocketed": "accelerated",
    "launches": "pushes off",
    "launch": "push off",
    "launching": "pushing off",
    "launched": "pushed off",
    "hurls": "throws",
    "hurl": "throw",
    "hurling": "throwing",
    "hurled": "threw",
    "flings": "tosses",
    "fling": "toss",
    "flinging": "tossing",
    "flung": "tossed",
    "catapults": "propels",
    "catapult": "propel",

    # --- Violent rotation ---
    "whips": "turns quickly",
    "whip": "turn quickly",
    "whipping": "turning quickly",
    "whipped": "turned quickly",
    "wrenches": "twists",
    "wrench": "twist",
    "wrenching": "twisting",
    "wrenched": "twisted",
    "jerks": "pulls sharply",
    "jerk": "pull sharply",
    "jerking": "pulling sharply",
    "jerked": "pulled sharply",

    # --- Chaotic locomotion ---
    "sprints": "runs",
    "sprint": "run",
    "sprinting": "running",
    "sprinted": "ran",
    "bolts": "runs",
    "bolt": "run",
    "bolting": "running",
    "bolted": "ran",
    "dashes": "runs",
    "dash": "run",
    "dashing": "running",
    "dashed": "ran",
    "charges": "advances",
    "charge": "advance",
    "charging": "advancing",
    "charged": "advanced",
    "barrels": "moves forward",
    "barrel": "move forward",
    "barreling": "moving forward",
    "barreled": "moved forward",
    "plunges": "moves downward",
    "plunge": "move downward",
    "plunging": "moving downward",
    "plunged": "moved downward",
    "scrambles": "climbs hastily",
    "scramble": "climb hastily",
    "scrambling": "climbing hastily",
    "scrambled": "climbed hastily",
    "lunges": "steps forward",
    "lunge": "step forward",
    "lunging": "stepping forward",
    "lunged": "stepped forward",
    "leaps": "jumps",
    "leap": "jump",
    "leaping": "jumping",
    "leapt": "jumped",
    "vaults": "climbs over",
    "vault": "climb over",
    "vaulting": "climbing over",
    "vaulted": "climbed over",

    # --- Dramatic body mechanics ---
    "collapses": "drops to the ground",
    "collapse": "drop to the ground",
    "collapsing": "dropping to the ground",
    "collapsed": "dropped to the ground",
    "staggers": "steps unevenly",
    "stagger": "step unevenly",
    "staggering": "stepping unevenly",
    "staggered": "stepped unevenly",
    "stumbles": "loses footing",
    "stumble": "lose footing",
    "stumbling": "losing footing",
    "stumbled": "lost footing",
    "recoils": "pulls back",
    "recoil": "pull back",
    "recoiling": "pulling back",
    "recoiled": "pulled back",
    "thrashes": "moves erratically",
    "thrash": "move erratically",
    "thrashing": "moving erratically",
    "thrashed": "moved erratically",

    # --- Emotional/dramatic ---
    "screams": "opens mouth wide",
    "scream": "open mouth wide",
    "screaming": "opening mouth wide",
    "screamed": "opened mouth wide",
    "gasps": "inhales sharply",
    "gasp": "inhale sharply",
    "gasping": "inhaling sharply",
    "gasped": "inhaled sharply",

    # --- Grasping violence ---
    "snatches": "grabs",
    "snatch": "grab",
    "snatching": "grabbing",
    "snatched": "grabbed",
    "yanks": "pulls firmly",
    "yank": "pull firmly",
    "yanking": "pulling firmly",
    "yanked": "pulled firmly",
    "rips": "pulls away",
    "rip": "pull away",
    "ripping": "pulling away",
    "ripped": "pulled away",
    "claws": "scrapes",
    "claw": "scrape",
    "clawing": "scraping",
    "clawed": "scraped",
}

# Pre-compile patterns sorted by length (longest first to avoid partial matches)
_COMPILED_PATTERNS: list[tuple[re.Pattern, str]] = [
    (re.compile(r'\b' + re.escape(verb) + r'\b', re.IGNORECASE), replacement)
    for verb, replacement in sorted(
        VERB_CALIBRATION.items(), key=lambda x: len(x[0]), reverse=True
    )
]


def calibrate_verbs(prompt: str) -> str:
    """Apply deterministic verb calibration to a prompt string.

    Replaces aggressive/dramatic verbs with physics-grounded equivalents.
    Uses whole-word matching. Preserves capitalization at sentence starts.
    """
    result = prompt
    for pattern, replacement in _COMPILED_PATTERNS:
        def _replace(match, repl=replacement):
            if match.group(0)[0].isupper():
                return repl[0].upper() + repl[1:]
            return repl
        result = pattern.sub(_replace, result)
    return result
