"""Fourier config — paths, dev token, version. Env-overridable, sane defaults.

Flat-module layout (run from this dir): the service imports `config` and
`engines.*` directly, matching the tide-table analyzers' sibling-import style.
"""
import os
from pathlib import Path

# ── identity ──────────────────────────────────────────────────────────────────
SERVICE = "fourier"                 # internal codename; the public sub-API is "audio".
VERSION = "v1"                       # the audio sub-API's own version (independent).

# api.nech.pl is a multi-API PLATFORM (SRE update 2026-06-25): Fourier is the
# `audio` sub-API, served publicly at /audio/v1/... The gateway strips that prefix
# and proxies to us at root (so internally routes are /healthz, /analyze/emotion…).
# root_path makes FastAPI advertise the public paths in /openapi.json + docs.
ROOT_PATH = os.environ.get("FOURIER_ROOT_PATH", "/audio/v1")

# ── central auth (platform-owned) ────────────────────────────────────────────
# The platform does CENTRAL auth + metering: nginx `auth_request` validates the
# bearer at the edge and injects the caller identity as these headers. We bind
# loopback-only (reachable only via the gateway) and TRUST them — no token store
# of our own (supersedes #23/#24). See SRE.md.
TENANT_HEADER = os.environ.get("FOURIER_TENANT_HEADER", "X-Tenant")
SCOPES_HEADER = os.environ.get("FOURIER_SCOPES_HEADER", "X-Scopes")

# ── repo-relative paths ─────────────────────────────────────────────────────────
HERE = Path(__file__).resolve().parent          # armada/api
ARMADA = HERE.parent                             # armada
TIDE_TABLE = ARMADA / "tide-table"               # the ears engines live here
FOUNDRY = ARMADA.parent / "tools" / "foundry"    # the Foundry engines

# ── cache / data roots (transient — freebox is the SSOT later) ───────────────────
# Default to a local dir for dev; point CACHE_ROOT at the freebox mount in prod.
CACHE_ROOT = Path(os.environ.get("FOURIER_CACHE", HERE / "_cache"))
DB_PATH = Path(os.environ.get("FOURIER_DB", HERE / "_cache" / "fourier.db"))

# ── auth ─────────────────────────────────────────────────────────────────────
# WALKING-SKELETON stub (#36): one hardcoded bearer from the env, swapped for the
# SQLite token store in #23. No token set → the guarded routes return 503 (not
# wide open) so we never accidentally ship an unauthenticated endpoint.
DEV_TOKEN = os.environ.get("FOURIER_DEV_TOKEN", "")

# ── limits ─────────────────────────────────────────────────────────────────────
MAX_UPLOAD_MB = int(os.environ.get("FOURIER_MAX_UPLOAD_MB", "40"))

# ── artifact store (#27) — local disk on erable; freebox deferred ───────────────
# Big binaries (fetched sources, stems, loops) live content-addressed under here,
# tracked as cache rows (kind→result_ref) and LRU-evicted to stay under the cap.
ARTIFACTS_ROOT = Path(os.environ.get("FOURIER_ARTIFACTS", CACHE_ROOT / "artifacts"))
ARTIFACTS_MAX_MB = int(os.environ.get("FOURIER_ARTIFACTS_MAX_MB", "1024"))   # 1 GB cap
# Serving: in prod nginx sends the bytes via X-Accel-Redirect (the app never
# streams big files itself). The header points at this INTERNAL nginx location,
# which maps to ARTIFACTS_ROOT. Off by default → dev serves a FileResponse.
X_ACCEL = os.environ.get("FOURIER_X_ACCEL", "").lower() in ("1", "true", "yes")
X_ACCEL_LOCATION = os.environ.get("FOURIER_X_ACCEL_LOCATION", "/_artifacts")

# ── /sources fetch (#29) — yt-dlp as a worker job ───────────────────────────────
# The worker is the ONLY process that shells out to yt-dlp. URL scope is OPEN
# (any http(s)), gated by the platform apikey + nechapi monitoring; we still hard-
# block SSRF footguns (file://, localhost, RFC1918/link-local) in sources.py.
YTDLP_BIN = os.environ.get("FOURIER_YTDLP", "yt-dlp")
FETCH_TIMEOUT_SEC = int(os.environ.get("FOURIER_FETCH_TIMEOUT", "180"))
FETCH_MAX_MB = int(os.environ.get("FOURIER_FETCH_MAX_MB", "60"))   # per-download cap
FETCH_SR = int(os.environ.get("FOURIER_FETCH_SR", "44100"))        # transcode target

# ── engine selection ─────────────────────────────────────────────────────────
# "clap"  = CLAP/torch (rich, ~4 GB, needs the GPU/dev box) — default for local dev.
# "light" = librosa-heuristic V/A (no torch, fits the erable container) — set in prod.
EMOTION_ENGINE = os.environ.get("FOURIER_EMOTION_ENGINE", "clap").lower()
