"""App configuration (pydantic-settings).

Values come from the process environment — in the container via the
compose `env_file`s (.env + .supabase-export.env); for host-side
tooling (alembic, scripts) the repo-root .env is read directly.
"""

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=".env", env_file_encoding="utf-8", extra="ignore"
    )

    app_name: str = "with"

    # The DEFAULT tenant's slug. Since ADR #015 the current tenant is
    # request-scoped (engagement-derived membership + the `with_tenant`
    # cookie), so this is NO LONGER a hard single-tenant pin: it is read in
    # exactly one place — ordering a super admin's allowed-tenant list so
    # his own books open first — and serves as the sensible default for
    # single-tenant DBs and tests. Nothing else reads it.
    app_tenant_slug: str = "bowden-works"

    # Satellite registry API (/api/registry/*) service token (ADR #014).
    # The shared secret every satellite presents as `Authorization: Bearer
    # <token>`. Empty = the registry FAILS CLOSED (503 REGISTRY_DISABLED) —
    # the surface is off until the owner provisions the token out-of-band
    # (staged in /srv/apps/with/.registry.env, key REGISTRY_SERVICE_TOKEN,
    # mode 600). Never logged or echoed.
    registry_service_token: str = ""

    # Sidecar Postgres (SQLAlchemy URLs, postgresql+psycopg driver).
    with_db_url: str = ""  # in-container: ...@db:5432/with
    with_db_url_host: str = ""  # host-side: ...@127.0.0.1:54317/with
    with_db_password: str = ""  # only used to redact secrets from error text

    # Nightly snapshot + rehearsal job (scripts/nightly_snapshot.py).
    snapshot_enabled: bool = True
    snapshot_time: str = "03:30"  # local time (container TZ, compose sets it)
    # Force the full pipeline at boot even when a legacy snapshot exists
    # (it always runs at boot when the legacy schema is missing).
    run_snapshot_on_start: bool = False

    # Supabase read-only source (.supabase-export.env) — snapshot job only.
    supabase_db_host: str = ""
    supabase_db_port: int = 5432
    supabase_db_user: str = ""
    supabase_db_name: str = ""
    supabase_db_password: str = ""


settings = Settings()
