import os

from pipx import paths
from pipx.backends import env_default_backend, find_uv_binary, resolve_backend_name
from pipx.constants import EXIT_CODE_OK, ExitCode
from pipx.emojis import EMOJI_SUPPORT
from pipx.interpreter import DEFAULT_PYTHON
from pipx.shared_libs import (
    DISABLE_SHARED_LIBS_AUTO_UPGRADE,
    shared_libs_auto_upgrade_disabled,
)
from pipx.util import PipxError

ENVIRONMENT_VARIABLES = [
    "PIPX_HOME",
    "PIPX_GLOBAL_HOME",
    "PIPX_BIN_DIR",
    "PIPX_GLOBAL_BIN_DIR",
    "PIPX_MAN_DIR",
    "PIPX_GLOBAL_MAN_DIR",
    "PIPX_SHARED_LIBS",
    "PIPX_DEFAULT_PYTHON",
    "PIPX_DEFAULT_BACKEND",
    "PIPX_FETCH_MISSING_PYTHON",
    "PIPX_FETCH_PYTHON",
    DISABLE_SHARED_LIBS_AUTO_UPGRADE,
    "PIPX_USE_EMOJI",
    "PIPX_HOME_ALLOW_SPACE",
]
DERIVED_ENVIRONMENT_VARIABLES = [
    "PIPX_LOCAL_VENVS",
    "PIPX_LOG_DIR",
    "PIPX_TRASH_DIR",
    "PIPX_VENV_CACHEDIR",
    "PIPX_STANDALONE_PYTHON_CACHEDIR",
    "PIPX_RESOLVED_BACKEND",
    "PIPX_BACKEND_SOURCE",
    "PIPX_UV_BINARY",
    "UV_CACHE_DIR",
]
ENVIRONMENT_VALUE_CHOICES = ENVIRONMENT_VARIABLES + DERIVED_ENVIRONMENT_VARIABLES


def environment(value: str) -> ExitCode:
    """Print a list of environment variables and paths used by pipx"""
    resolved_backend, backend_source = resolve_backend_name(env_value=env_default_backend())
    uv_binary, _uv_source = find_uv_binary()
    derived_values = {
        "PIPX_HOME": paths.ctx.home,
        "PIPX_BIN_DIR": paths.ctx.bin_dir,
        "PIPX_MAN_DIR": paths.ctx.man_dir,
        "PIPX_SHARED_LIBS": paths.ctx.shared_libs,
        "PIPX_LOCAL_VENVS": paths.ctx.venvs,
        "PIPX_LOG_DIR": paths.ctx.logs,
        "PIPX_TRASH_DIR": paths.ctx.trash,
        "PIPX_VENV_CACHEDIR": paths.ctx.venv_cache,
        "PIPX_STANDALONE_PYTHON_CACHEDIR": paths.ctx.standalone_python_cachedir,
        "PIPX_DEFAULT_PYTHON": DEFAULT_PYTHON,
        "PIPX_RESOLVED_BACKEND": resolved_backend,
        "PIPX_BACKEND_SOURCE": backend_source,
        "PIPX_UV_BINARY": str(uv_binary) if uv_binary else "",
        "UV_CACHE_DIR": os.environ.get("UV_CACHE_DIR", ""),
        DISABLE_SHARED_LIBS_AUTO_UPGRADE: str(shared_libs_auto_upgrade_disabled()).lower(),
        "PIPX_USE_EMOJI": str(EMOJI_SUPPORT).lower(),
        "PIPX_HOME_ALLOW_SPACE": str(paths.ctx.allow_spaces_in_home_path).lower(),
    }
    if value is None:
        print("Environment variables (set by user):")  # type: ignore[unreachable]
        print("")
        for env_variable in ENVIRONMENT_VARIABLES:
            env_value = os.getenv(env_variable, "")
            print(f"{env_variable}={env_value}")
        print("")
        print("Derived values (computed by pipx):")
        print("")
        for env_variable, derived_value in derived_values.items():
            print(f"{env_variable}={derived_value}")
    elif value in derived_values:
        print(derived_values[value])
    else:
        raise PipxError("Variable not found.")

    return EXIT_CODE_OK
