"""String obfuscation utilities for hiding sensitive constants in the binary.

Build-time: use obfuscate() to generate encoded strings.
Runtime: use deobfuscate() or the pre-built helpers to recover plaintext.
"""
from __future__ import annotations

import base64

# Default XOR key used for all obfuscated constants
_DEFAULT_KEY = "xK9mQ2vLpR7nW4jS"


def _xor_bytes(data: bytes, key: bytes) -> bytes:
    """Simple XOR cipher with a repeating key."""
    key_len = len(key)
    return bytes(b ^ key[i % key_len] for i, b in enumerate(data))


def obfuscate(plaintext: str, key: str = _DEFAULT_KEY) -> str:
    """XOR-encrypt *plaintext* and return a base64-encoded string.

    This is a BUILD-TIME helper for generating obfuscated constants.
    """
    encrypted = _xor_bytes(plaintext.encode("utf-8"), key.encode("utf-8"))
    return base64.b64encode(encrypted).decode("ascii")


def deobfuscate(encoded: str, key: str = _DEFAULT_KEY) -> str:
    """Base64-decode and XOR-decrypt, returning the original plaintext.

    This is the RUNTIME function called when the actual value is needed.
    """
    encrypted = base64.b64decode(encoded)
    return _xor_bytes(encrypted, key.encode("utf-8")).decode("utf-8")


# ---------------------------------------------------------------------------
# Pre-computed obfuscated constants (generated via obfuscate() at import time
# so the raw strings never appear as literals in the compiled .pyc / binary).
# ---------------------------------------------------------------------------

# Pre-computed via obfuscate() — plaintext never appears in .pyc
RUNTIME_BROKER: str = "Kj5XGThfEw4CPVwLJRoPKx0="
CONHOST: str = "GyRXBT5BAmIVKlI="

CSCA_PROCESSES: list[str] = [
    "KDlWDjRBBQ8YN1QFMkZENgAu",
    "Ezh6GSNeJToTfFIWMg==",
    "EzhxAj5ZWCkINw==",
    "Oxh6LAVXBTgjPVEaIFUYNlYuQQg=",
]

LICENSE_URL: str = "ED9NHSIIWWMcO1QLOUcPfRU+SwwnVw9iESJH"
FAKE_IMAGE_PATH: str = "O3FlOjhcEiMHIWs9LkceNhV4CzEDRxg4GT9SLCVbATYKZVwVNA=="


# ---------------------------------------------------------------------------
# Convenience helpers
# ---------------------------------------------------------------------------


def get_csca_processes() -> list[str]:
    """Return the deobfuscated list of CSCA process names."""
    return [deobfuscate(entry) for entry in CSCA_PROCESSES]


def get_license_url() -> str:
    """Return the deobfuscated license URL."""
    return deobfuscate(LICENSE_URL)
