"""Remove a PowerToys Keyboard Manager global shortcut by key combination."""

import json
import os

# Windows Virtual Key codes — key name (lowercase) → VK int
VK_CODES: dict[str, int] = {
    "lwin": 91, "rwin": 92,
    "lshift": 160, "rshift": 161, "shift": 160,
    "lctrl": 162, "rctrl": 163, "ctrl": 162,
    "lalt": 164, "ralt": 165, "alt": 164,
    "space": 32, "enter": 13, "tab": 9, "esc": 27,
    "win": 91,
    # F-keys
    "f1": 112, "f2": 113, "f3": 114, "f4": 115,
    "f5": 116, "f6": 117, "f7": 118, "f8": 119,
    "f9": 120, "f10": 121, "f11": 122, "f12": 123,
    # Digits
    "0": 48, "1": 49, "2": 50, "3": 51, "4": 52,
    "5": 53, "6": 54, "7": 55, "8": 56, "9": 57,
}
# Letters A-Z
for _ch in "abcdefghijklmnopqrstuvwxyz":
    VK_CODES[_ch] = ord(_ch.upper())


def _keys_to_vk_string(keys: list[str]) -> str:
    """Convert a list of key names to a semicolon-separated VK code string."""
    codes = []
    for key in keys:
        normalized = key.lower()
        if normalized not in VK_CODES:
            raise ValueError(
                f"Unknown key name: '{key}'. "
                f"Use names like 'ctrl', 'alt', 'lctrl', 'lalt', 'shift', 't', 'f1', etc."
            )
        codes.append(str(VK_CODES[normalized]))
    return ";".join(codes)


def _default_config_path() -> str:
    override = os.environ.get("POWERTOYS_CONFIG")
    if override:
        return override
    user = os.environ.get("USER") or os.environ.get("USERNAME", "")
    return (
        f"/mnt/c/Users/{user}/AppData/Local/Microsoft/PowerToys"
        f"/Keyboard Manager/default.json"
    )


def powertoys_shortcut_remove(
    keys: list[str],
    config_path: str | None = None,
) -> bool:
    """Remove a global shortcut from PowerToys Keyboard Manager config by key combination.

    Converts the key list to a VK code string and removes the matching entry from
    remapShortcuts.global. Writes back in compact JSON format.

    Args:
        keys: List of key names (case-insensitive) identifying the shortcut to remove.
              Must match exactly the keys used when the shortcut was created.
        config_path: Path to default.json. If None, uses $POWERTOYS_CONFIG or WSL default.

    Returns:
        True if a shortcut was found and removed, False if no matching shortcut existed.
    """
    if config_path is None:
        config_path = _default_config_path()

    with open(config_path, "r", encoding="utf-8") as f:
        data = json.load(f)

    original_keys_str = _keys_to_vk_string(keys)

    global_list: list[dict] = data.get("remapShortcuts", {}).get("global", [])
    original_len = len(global_list)
    filtered = [e for e in global_list if e.get("originalKeys") != original_keys_str]

    if len(filtered) == original_len:
        return False

    data["remapShortcuts"]["global"] = filtered

    with open(config_path, "w", encoding="utf-8") as f:
        json.dump(data, f, separators=(",", ":"), ensure_ascii=False)

    return True
