"""Env-var configuration for the interpret service."""

from __future__ import annotations

import os
import sys
from dataclasses import dataclass


@dataclass(frozen=True)
class Config:
    host: str
    port: int
    anthropic_api_key: str
    anthropic_model: str
    interpretation_max_retries: int
    interpretation_timeout_s: int
    # Where the api stores uploaded images. We read directly from the
    # filesystem since both services run on the same host in Phase 1.
    upload_dir: str


def load() -> Config:
    api_key = os.environ.get("ANTHROPIC_API_KEY", "").strip()
    if not api_key:
        # Allow boot in dev/test without a key — but routes will return 503.
        # Logging is fine via stdout because systemd captures it.
        print(
            "warn: ANTHROPIC_API_KEY not set; /v1/interpret will return 503",
            file=sys.stderr,
        )
    return Config(
        host=os.environ.get("HOST", "127.0.0.1"),
        port=int(os.environ.get("PORT", "3112")),
        anthropic_api_key=api_key,
        anthropic_model=os.environ.get("EKG_TUTOR_INTERPRET_MODEL", "claude-sonnet-4-6"),
        interpretation_max_retries=int(
            os.environ.get("EKG_TUTOR_INTERPRET_MAX_RETRIES", "1")
        ),
        interpretation_timeout_s=int(os.environ.get("EKG_TUTOR_INTERPRET_TIMEOUT_S", "90")),
        upload_dir=os.environ.get("EKG_TUTOR_UPLOAD_DIR", "/var/lib/ekg-tutor/uploads"),
    )


config = load()
