"""Configuration settings for AI Insights Service."""

from shared.config.base import BaseServiceSettings
import os
from typing import Optional


class Settings(BaseServiceSettings):
    """Application settings."""

    # Service Info
    SERVICE_NAME: str = "ai-insights"
    SERVICE_VERSION: str = "1.0.0"
    API_V1_PREFIX: str = "/api/v1"

    # Database configuration (secure approach - build from components)
    @property
    def DATABASE_URL(self) -> str:
        """Build database URL from secure components"""
        # Try complete URL first (for backward compatibility)
        complete_url = os.getenv("AI_INSIGHTS_DATABASE_URL")
        if complete_url:
            return complete_url

        # Also check for generic DATABASE_URL (for migration compatibility)
        generic_url = os.getenv("DATABASE_URL")
        if generic_url:
            return generic_url

        # Build from components (secure approach)
        user = os.getenv("AI_INSIGHTS_DB_USER", "ai_insights_user")
        password = os.getenv("AI_INSIGHTS_DB_PASSWORD", "ai_insights_pass123")
        host = os.getenv("AI_INSIGHTS_DB_HOST", "localhost")
        port = os.getenv("AI_INSIGHTS_DB_PORT", "5432")
        name = os.getenv("AI_INSIGHTS_DB_NAME", "ai_insights_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"

    DB_POOL_SIZE: int = 20
    DB_MAX_OVERFLOW: int = 10

    # Redis (inherited from BaseServiceSettings but can override)
    REDIS_CACHE_TTL: int = 900  # 15 minutes
    REDIS_DB: int = 3  # Dedicated Redis database for AI Insights

    # Service URLs
    FORECASTING_SERVICE_URL: str = "http://forecasting-service:8000"
    PROCUREMENT_SERVICE_URL: str = "http://procurement-service:8000"
    PRODUCTION_SERVICE_URL: str = "http://production-service:8000"
    SALES_SERVICE_URL: str = "http://sales-service:8000"
    INVENTORY_SERVICE_URL: str = "http://inventory-service:8000"

    # Circuit Breaker Settings
    CIRCUIT_BREAKER_FAILURE_THRESHOLD: int = 5
    CIRCUIT_BREAKER_TIMEOUT: int = 60

    # Insight Settings
    MIN_CONFIDENCE_THRESHOLD: int = 60
    DEFAULT_INSIGHT_TTL_DAYS: int = 7
    MAX_INSIGHTS_PER_REQUEST: int = 100

    # Feedback Settings
    FEEDBACK_PROCESSING_ENABLED: bool = True
    FEEDBACK_PROCESSING_SCHEDULE: str = "0 6 * * *"  # Daily at 6 AM

    # Logging
    LOG_LEVEL: str = "INFO"

    # CORS
    ALLOWED_ORIGINS: list[str] = ["http://localhost:3000", "http://localhost:5173"]

    class Config:
        env_file = ".env"
        case_sensitive = True


settings = Settings()
