"""Internal constants for the Baponi SDK."""

from __future__ import annotations

import re
from importlib.metadata import version as _pkg_version

VERSION = _pkg_version("baponi")
USER_AGENT = f"baponi-python/{VERSION}"
DEFAULT_BASE_URL = "https://api.baponi.ai"
EXECUTE_PATH = "/v1/sandbox/execute"
FILES_UPLOAD_URL_PATH = "/v1/files/upload_url"
FILES_DOWNLOAD_URL_PATH = "/v1/files/download_url"
FILES_LIST_PATH = "/v1/files/list"

DEFAULT_TIMEOUT = 60.0
DEFAULT_MAX_RETRIES = 2
DEFAULT_CONNECT_TIMEOUT = 10.0
DEFAULT_EXECUTION_TIMEOUT = 30
EXECUTION_READ_BUFFER = 10.0  # seconds added to read timeout beyond execution timeout

SUPPORTED_LANGUAGES = frozenset({"python", "bash", "node"})
MAX_CODE_SIZE = 1_048_576  # 1 MB
MAX_TIMEOUT = 3600
MIN_TIMEOUT = 1
MAX_THREAD_ID_LENGTH = 128
MAX_METADATA_KEYS = 10
MAX_METADATA_KEY_LENGTH = 40
MAX_METADATA_VALUE_LENGTH = 256

THREAD_ID_PATTERN = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_-]*$")
METADATA_KEY_PATTERN = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.\-]*$")

# Environment variable limits (mirrors backend env_vars.rs)
MAX_ENV_VARS_COUNT = 50
MAX_ENV_VAR_KEY_LENGTH = 128
MAX_ENV_VAR_VALUE_LENGTH = 4096
MAX_ENV_VARS_TOTAL_SIZE = 65536  # 64KB
ENV_VAR_KEY_PATTERN = re.compile(r"^[A-Z][A-Z0-9_]*$")

# Match backend env_vars.rs exactly. Generic error, don't leak list.
RESERVED_ENV_VAR_NAMES = frozenset({
    "PATH", "HOME", "TMPDIR", "PWD", "OLDPWD", "SHELL",
    "LANG", "LANGUAGE", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES",
    "LC_MONETARY", "LC_NUMERIC", "LC_TIME",
    "PYTHONPATH", "PYTHONHOME", "PYTHONSTARTUP", "PYTHONUSERBASE",
    "PYTHONUNBUFFERED", "PYTHONDONTWRITEBYTECODE", "PYTHONHASHSEED",
    "PYTHONWARNINGS", "PYTHONOPTIMIZE", "PYTHONINSPECT", "PYTHONDEBUG",
    "PYTHONIOENCODING", "PYTHONMALLOC", "PYTHONCOERCECLOCALE",
    "PYTHONFAULTHANDLER", "PYTHONPROFILEIMPORTTIME", "PYTHONPYCACHEPREFIX",
    "PIP_USER", "PIP_TARGET", "PIP_PREFIX",
    "LD_LIBRARY_PATH", "LD_PRELOAD", "LD_AUDIT", "LD_DEBUG", "LD_BIND_NOW",
    "LD_TRACE_LOADED_OBJECTS", "LD_PROFILE", "LD_DYNAMIC_WEAK",
    "BASH_ENV", "ENV", "CDPATH", "GLOBIGNORE", "BASH_XTRACEFD",
    "PS1", "PS2", "PS4", "PROMPT_COMMAND", "IFS",
    "HTTP_PROXY", "HTTPS_PROXY", "FTP_PROXY", "ALL_PROXY", "NO_PROXY",
    "NODE_PATH", "NODE_OPTIONS", "NODE_EXTRA_CA_CERTS",
    "NPM_CONFIG_PREFIX", "NPM_CONFIG_CACHE", "NPM_CONFIG_GLOBALCONFIG",
    "CARGO_HOME", "RUSTUP_HOME",
    "GOPATH", "GOMODCACHE", "GOBIN",
    "R_LIBS_USER", "JULIA_DEPOT_PATH",
    "TERM", "USER", "LOGNAME", "MAIL", "EDITOR", "VISUAL",
})
RESERVED_ENV_VAR_PREFIXES = ("BAPONI_", "NSJAIL_", "LD_", "LC_", "XDG_", "_")

# Execution status endpoints
EXECUTIONS_PATH = "/v1/executions"
TERMINAL_STATUSES = frozenset({
    "success", "failed", "timeout", "cancelled", "blocked", "oom", "seccomp", "deepguard",
})
DEFAULT_POLL_INTERVAL = 2.0
