"""``orchestrator.secrets`` — the short, documented name for the secrets vault.

The implementation lives in :mod:`orchestrator.secrets_vault` (one store, one
file, one audit rule). This module is the façade the brief asks for: the four
functions an adapter needs, spelled the obvious way, plus ``redact()`` for
anything that could end up in a log line, a ledger row, a claim or a response.

The API
-------
    get(name)            -> str | None      the raw value, for one purpose only:
                                            putting the credential on the wire
    set(name, value)                        store/replace; audited by NAME only
    has(name)            -> bool
    names()              -> list[str]       CONFIGURED names. Never values.
    delete(name)         -> bool
    redact(value)        -> str             "pk.e…" — safe to print anywhere

Where it is stored
------------------
    $JARVIS_SECRETS_PATH   (or $JARVIS_SECRETS_FILE — both accepted)
    ~/.jarvis/secrets.json (default)

Always outside the project tree, directory ``0700``, file ``0600``, and the
``.gitignore`` entry is already in place. Resolution order for ``get()``:

    1. the environment (``MAPBOX_TOKEN``, ``MAPBOX_API_KEY``, ...)
    2. the vault file
    3. ``None``

A key that is absent is reported as **NEEDS KEY** by the adapters — never as a
zero, never as a guess, never as an exception that takes a page down.

What this module will not do
----------------------------
There is no function here that returns more than a name for an unnamed caller,
and no Flask route imports it beyond ``status_rows()`` (names + booleans). A
value is not serialisable out of this module by accident: nothing here returns
a whole mapping of keys, only ``names()`` and per-name lookups.
"""

from __future__ import annotations

from . import secrets_vault
from .secrets_vault import (  # re-exported: one implementation, one file
    KNOWN_KEYS,
    MISSING,
    PRESENT,
    PATH_ENV,
    SecretsError,
    path_display,
    redact,
    scrub,
    secrets_path,
    source,
    status,
    summary,
)

__all__ = [
    "KNOWN_KEYS",
    "MISSING",
    "PRESENT",
    "PATH_ENV",
    "SecretsError",
    "delete",
    "get",
    "has",
    "names",
    "path_display",
    "redact",
    "scrub",
    "secrets_path",
    "set",
    "source",
    "status",
    "status_rows",
    "summary",
]


def get(name: str) -> str | None:
    """The raw value (environment first, then the vault file), or ``None``.

    Never logs, never audits, never prints. The only caller that needs this is
    the HTTP adapter that puts the credential on the wire.
    """
    return secrets_vault.get(name)


def set(name, value) -> None:  # noqa: A001 - the documented API name
    """Store/replace one credential.

    The value goes to the ``0600`` vault file and nowhere else: the audit row
    carries the NAME and ``present: true``, the log line carries the name and a
    character count, and the return value is ``None``.
    """
    secrets_vault.set(name, value)


def has(name: str) -> bool:
    """Is a value available for ``name`` (from either layer)?"""
    return secrets_vault.has(name)


def names() -> list[str]:
    """The CONFIGURED key names — every name that ``has()`` says yes to.

    Names only. There is no argument, no flag and no variant of this function
    that returns a value.
    """
    return [name for name in secrets_vault.list_names() if secrets_vault.has(name)]


def delete(name: str) -> bool:
    """Remove a stored credential. ``True`` if something was there."""
    return secrets_vault.delete(name)


def status_rows() -> list[dict]:
    """``[{"name": ..., "configured": bool}, ...]`` for every known key name.

    This is the *entire* payload of ``GET /secrets/status``: a name and a
    boolean per key, nothing else. No value, no slice of a value, no length.
    """
    rows = []
    for name in secrets_vault.list_names():
        rows.append({"name": name, "configured": secrets_vault.has(name)})
    return rows
