# services/inventory/app/core/config.py
"""
Inventory Service Configuration
"""

import os
from typing import List
from pydantic import Field
from shared.config.base import BaseServiceSettings


class Settings(BaseServiceSettings):
    """Inventory service settings extending base configuration"""
    
    # Override service-specific settings
    SERVICE_NAME: str = "inventory-service"
    VERSION: str = "1.0.0"
    APP_NAME: str = "Bakery Inventory Service"
    DESCRIPTION: str = "Inventory and stock management service"
    
    # API Configuration
    API_V1_STR: 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("INVENTORY_DATABASE_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("INVENTORY_DB_USER", "inventory_user")
        password = os.getenv("INVENTORY_DB_PASSWORD", "inventory_pass123")
        host = os.getenv("INVENTORY_DB_HOST", "localhost")
        port = os.getenv("INVENTORY_DB_PORT", "5432")
        name = os.getenv("INVENTORY_DB_NAME", "inventory_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
    
    # Inventory-specific Redis database
    REDIS_DB: int = Field(default=3, env="INVENTORY_REDIS_DB")
    
    # File upload configuration
    MAX_UPLOAD_SIZE: int = 10 * 1024 * 1024  # 10MB
    UPLOAD_PATH: str = Field(default="/tmp/uploads", env="INVENTORY_UPLOAD_PATH")
    ALLOWED_FILE_EXTENSIONS: List[str] = [".csv", ".xlsx", ".xls", ".png", ".jpg", ".jpeg"]
    
    # Pagination
    DEFAULT_PAGE_SIZE: int = 50
    MAX_PAGE_SIZE: int = 1000
    
    # Stock validation
    MIN_QUANTITY: float = 0.0
    MAX_QUANTITY: float = 100000.0
    MIN_PRICE: float = 0.01
    MAX_PRICE: float = 10000.0
    
    # Inventory-specific cache TTL
    INVENTORY_CACHE_TTL: int = 180  # 3 minutes for real-time stock
    INGREDIENT_CACHE_TTL: int = 600  # 10 minutes
    SUPPLIER_CACHE_TTL: int = 1800   # 30 minutes
    
    # Low stock thresholds
    DEFAULT_LOW_STOCK_THRESHOLD: int = 10
    DEFAULT_REORDER_POINT: int = 20
    DEFAULT_REORDER_QUANTITY: int = 50
    
    # Expiration alert thresholds (in days)
    EXPIRING_SOON_DAYS: int = 7
    EXPIRED_ALERT_DAYS: int = 1
    
    # Barcode/QR configuration
    BARCODE_FORMAT: str = "Code128"
    QR_CODE_VERSION: int = 1
    
    # Food safety and compliance settings
    FOOD_SAFETY_ENABLED: bool = Field(default=True, env="FOOD_SAFETY_ENABLED")
    TEMPERATURE_MONITORING_ENABLED: bool = Field(default=True, env="TEMPERATURE_MONITORING_ENABLED")
    AUTOMATIC_COMPLIANCE_CHECKS: bool = Field(default=True, env="AUTOMATIC_COMPLIANCE_CHECKS")
    
    # Temperature monitoring thresholds
    REFRIGERATION_TEMP_MIN: float = Field(default=1.0, env="REFRIGERATION_TEMP_MIN")  # Celsius
    REFRIGERATION_TEMP_MAX: float = Field(default=4.0, env="REFRIGERATION_TEMP_MAX")  # Celsius
    FREEZER_TEMP_MIN: float = Field(default=-20.0, env="FREEZER_TEMP_MIN")  # Celsius
    FREEZER_TEMP_MAX: float = Field(default=-15.0, env="FREEZER_TEMP_MAX")  # Celsius
    ROOM_TEMP_MIN: float = Field(default=18.0, env="ROOM_TEMP_MIN")  # Celsius
    ROOM_TEMP_MAX: float = Field(default=25.0, env="ROOM_TEMP_MAX")  # Celsius
    
    # Temperature alert thresholds
    TEMP_DEVIATION_ALERT_MINUTES: int = Field(default=15, env="TEMP_DEVIATION_ALERT_MINUTES")
    CRITICAL_TEMP_DEVIATION_MINUTES: int = Field(default=5, env="CRITICAL_TEMP_DEVIATION_MINUTES")
    TEMP_SENSOR_OFFLINE_ALERT_MINUTES: int = Field(default=30, env="TEMP_SENSOR_OFFLINE_ALERT_MINUTES")
    
    # Food safety alert thresholds
    EXPIRATION_WARNING_DAYS: int = Field(default=3, env="EXPIRATION_WARNING_DAYS")
    CRITICAL_EXPIRATION_HOURS: int = Field(default=24, env="CRITICAL_EXPIRATION_HOURS")
    QUALITY_SCORE_THRESHOLD: float = Field(default=8.0, env="QUALITY_SCORE_THRESHOLD")
    
    # Compliance monitoring
    AUDIT_REMINDER_DAYS: int = Field(default=30, env="AUDIT_REMINDER_DAYS")
    CERTIFICATION_EXPIRY_WARNING_DAYS: int = Field(default=60, env="CERTIFICATION_EXPIRY_WARNING_DAYS")
    COMPLIANCE_CHECK_FREQUENCY_HOURS: int = Field(default=24, env="COMPLIANCE_CHECK_FREQUENCY_HOURS")
    
    # Dashboard refresh intervals
    DASHBOARD_CACHE_TTL: int = Field(default=300, env="DASHBOARD_CACHE_TTL")  # 5 minutes
    ALERTS_REFRESH_INTERVAL: int = Field(default=60, env="ALERTS_REFRESH_INTERVAL")  # 1 minute
    TEMPERATURE_LOG_INTERVAL: int = Field(default=300, env="TEMPERATURE_LOG_INTERVAL")  # 5 minutes
    
    # Alert notification settings
    ENABLE_EMAIL_ALERTS: bool = Field(default=True, env="ENABLE_EMAIL_ALERTS")
    ENABLE_SMS_ALERTS: bool = Field(default=True, env="ENABLE_SMS_ALERTS")
    ENABLE_WHATSAPP_ALERTS: bool = Field(default=True, env="ENABLE_WHATSAPP_ALERTS")
    REGULATORY_NOTIFICATION_ENABLED: bool = Field(default=False, env="REGULATORY_NOTIFICATION_ENABLED")
    
    # Business model detection for inventory
    ENABLE_BUSINESS_MODEL_DETECTION: bool = Field(default=True, env="ENABLE_BUSINESS_MODEL_DETECTION")
    CENTRAL_BAKERY_THRESHOLD_INGREDIENTS: int = Field(default=50, env="CENTRAL_BAKERY_THRESHOLD_INGREDIENTS")
    INDIVIDUAL_BAKERY_THRESHOLD_INGREDIENTS: int = Field(default=20, env="INDIVIDUAL_BAKERY_THRESHOLD_INGREDIENTS")


# Global settings instance
settings = Settings()