"""The environment the kanban dispatcher hands its worker client: an allowlist, not a scrub.

The client (``hermes_cli.kanban_worker_client``) only submits a claim to the assignee's gateway and
waits for the receipt. When that gateway is not running, the client starts it, and the daemon is
built from the client's own environment plus the assignee's secrets
(``gateway_runtime_start`` -> ``served_profile_child_env``). Anything else the client carries
therefore reaches the assignee's turns, and an overlay can replace a value but never remove one. A
scrub of the dispatcher's environment only catches the names it knows (provider keys, the
dispatcher's ``terminal.env_passthrough``); a token systemd or ``op run`` injected under any other
name went through. So the dispatcher's environment is reduced to host process plumbing and
Hermes' own location before the dispatcher adds the claim.
"""

from __future__ import annotations

from pathlib import Path
from typing import Mapping

# Exact names (Windows matches case-insensitively). Host plumbing, never a profile's settings.
_HOST_NAMES = frozenset({
    "PATH", "HOME", "USER", "LOGNAME", "SHELL", "TERM", "TZ", "LANG", "LANGUAGE",
    "TMPDIR", "TMP", "TEMP", "PYTHONPATH", "PYTHONUTF8", "PYTHONIOENCODING", "VIRTUAL_ENV", "CONDA_PREFIX",
    # The user systemd manager (a gateway started from a system unit still scopes its children).
    "DBUS_SESSION_BUS_ADDRESS", "SUDO_USER",
    # The login session's agent and display sockets (git over ssh, browser/computer-use tools).
    "SSH_AUTH_SOCK", "DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY",
    # TLS trust and proxies are the host's network, which a cold-started gateway needs to reach a provider.
    "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", "NODE_EXTRA_CA_CERTS",
    "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
    # Windows: without these the CRT cannot open a socket or resolve cmd.exe.
    "SYSTEMROOT", "SYSTEMDRIVE", "WINDIR", "COMSPEC", "PATHEXT", "OS", "PROCESSOR_ARCHITECTURE",
    "NUMBER_OF_PROCESSORS", "PUBLIC", "ALLUSERSPROFILE", "PROGRAMDATA", "PROGRAMFILES",
    "PROGRAMFILES(X86)", "PROGRAMW6432", "APPDATA", "LOCALAPPDATA", "USERPROFILE", "USERDOMAIN",
    "USERNAME", "HOMEDRIVE", "HOMEPATH", "COMPUTERNAME", "PROCESSOR_ARCHITEW6432",
    # Hermes' own location, install and host policy, read before any profile config loads: the
    # host-wide gateway lock directory (else a cold-started gateway takes a different "host" lock),
    # the no-install policy, the state folder a launcher named, and the shared board root and its
    # lock's busy timeout.
    "HERMES_HOME", "HERMES_REAL_HOME", "HERMES_SCRATCH_DIR", "HERMES_RUNTIME_DIR", "HERMES_INSTALL_ROOT",
    "HERMES_PYTHON_SRC_ROOT", "HERMES_DATA_DIR_SUFFIX", "HERMES_HOME_MODE", "HERMES_MANAGED",
    "HERMES_CONTAINER", "HERMES_SKIP_CHMOD", "HERMES_GATEWAY_LOCK_DIR", "HERMES_DISABLE_LAZY_INSTALLS",
    "HERMES_INSTALL_STATE_DIR", "HERMES_KANBAN_HOME", "HERMES_KANBAN_BUSY_TIMEOUT_MS",
    # The administrator-managed scope directory (an IT bootstrap override, never in any profile's
    # ``.env``): without it a cold-started gateway loses the managed overlay stored elsewhere.
    "HERMES_MANAGED_DIR",
    # The XDG base directories by exact name (state: the host gateway lock; config: the package
    # index). Not the XDG_ prefix: XDG_ACTIVATION_TOKEN and any user-chosen XDG_* name can be a secret.
    "XDG_RUNTIME_DIR", "XDG_STATE_HOME", "XDG_CONFIG_HOME", "XDG_CONFIG_DIRS", "XDG_DATA_HOME",
    "XDG_DATA_DIRS", "XDG_CACHE_HOME",
    # Termux detection (hermes_platform.host.runtime).
    "PREFIX", "TERMUX_VERSION",
})
_HOST_PREFIXES = ("LC_",)


def worker_client_env(env: Mapping[str, str], assignee_home: str | Path) -> dict[str, str]:
    """*env* reduced to what the worker client for *assignee_home*, and a gateway it starts, may take
    from the host. The launch profile's ``.env`` / source residue goes first: an allowlisted name it
    defines (an ``HTTPS_PROXY`` with credentials in the URL) is that profile's, not the host's."""
    from hermes_cli.config_backend import deployment_env_names
    from tools.environments.local import strip_launch_profile_env
    host = strip_launch_profile_env(dict(env), assignee_home)
    # The config backend's deployment (which backend, plane and instance) is the instance's, not a
    # profile's; without it the assignee's gateway reads its config from the file backend. Its
    # credential is not part of it: the assignee resolves its own and fails closed without it.
    names = _HOST_NAMES | {name.upper() for name in deployment_env_names()}
    return {k: v for k, v in host.items() if k.upper() in names or k.upper().startswith(_HOST_PREFIXES)}
