import os
from typing import Optional


class Settings:
    # Database settings
    SQLITE_DB_PATH: str = os.getenv("SQLITE_DB_PATH", "/var/lib/sqlite/metrics.db")
    DATABASE_URL: str = os.getenv("DATABASE_URL", f"sqlite:///{SQLITE_DB_PATH}")
    METRICS_RETENTION_DAYS: int = int(os.getenv("METRICS_RETENTION_DAYS", "90"))
    DB_CONNECTION_TIMEOUT: int = int(os.getenv("DB_CONNECTION_TIMEOUT", "30"))
    DB_MAX_RETRIES: int = int(os.getenv("DB_MAX_RETRIES", "5"))

    # Service settings
    METRICS_SERVICE_PORT: int = int(os.getenv("METRICS_SERVICE_PORT", "8890"))
    # Service binds to 0.0.0.0 for container/K8s deployment where network isolation
    # is provided by container runtime and ingress controllers.
    METRICS_SERVICE_HOST: str = os.getenv("METRICS_SERVICE_HOST", "0.0.0.0")  # nosec B104 - intentional for containerized deployment

    # OpenTelemetry settings
    OTEL_SERVICE_NAME: str = os.getenv("OTEL_SERVICE_NAME", "mcp-metrics-service")
    OTEL_PROMETHEUS_ENABLED: bool = os.getenv("OTEL_PROMETHEUS_ENABLED", "true").lower() == "true"
    OTEL_PROMETHEUS_PORT: int = int(os.getenv("OTEL_PROMETHEUS_PORT", "9465"))
    OTEL_OTLP_ENDPOINT: Optional[str] = os.getenv("OTEL_OTLP_ENDPOINT")
    OTEL_OTLP_EXPORT_INTERVAL_MS: int = int(os.getenv("OTEL_OTLP_EXPORT_INTERVAL_MS", "30000"))

    # API Security
    METRICS_RATE_LIMIT: int = int(os.getenv("METRICS_RATE_LIMIT", "1000"))
    API_KEY_HASH_ALGORITHM: str = os.getenv("API_KEY_HASH_ALGORITHM", "sha256")

    # Histogram bucket boundaries for duration metrics (seconds)
    HISTOGRAM_BUCKET_BOUNDARIES: list = [
        float(x)
        for x in os.getenv(
            "HISTOGRAM_BUCKET_BOUNDARIES",
            "0.005,0.01,0.025,0.05,0.1,0.25,0.5,1.0,2.5,5.0,10.0,30.0,60.0,120.0,300.0",
        ).split(",")
    ]

    # Performance
    BATCH_SIZE: int = int(os.getenv("BATCH_SIZE", "100"))
    FLUSH_INTERVAL_SECONDS: int = int(os.getenv("FLUSH_INTERVAL_SECONDS", "30"))
    MAX_REQUEST_SIZE: str = os.getenv("MAX_REQUEST_SIZE", "10MB")


settings = Settings()
