# ================================================================
# NOTIFICATION SERVICE CONFIGURATION
# services/notification/app/core/config.py
# ================================================================

"""
Notification service configuration
Email and WhatsApp notification handling
"""

from shared.config.base import BaseServiceSettings
import os

class NotificationSettings(BaseServiceSettings):
    """Notification service specific settings"""
    
    # Service Identity
    APP_NAME: str = "Notification Service"
    SERVICE_NAME: str = "notification-service"
    DESCRIPTION: str = "Email and WhatsApp notification service"
    
    # 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("NOTIFICATION_DATABASE_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("NOTIFICATION_DB_USER", "notification_user")
        password = os.getenv("NOTIFICATION_DB_PASSWORD", "notification_pass123")
        host = os.getenv("NOTIFICATION_DB_HOST", "localhost")
        port = os.getenv("NOTIFICATION_DB_PORT", "5432")
        name = os.getenv("NOTIFICATION_DB_NAME", "notification_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
    
    # Redis Database (dedicated for notification queue)
    REDIS_DB: int = 5
    
    # Email Configuration
    SMTP_HOST: str = os.getenv("SMTP_HOST", "smtp.gmail.com")
    SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
    SMTP_USER: str = os.getenv("SMTP_USER", "")
    SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
    SMTP_TLS: bool = os.getenv("SMTP_TLS", "true").lower() == "true"
    SMTP_SSL: bool = os.getenv("SMTP_SSL", "false").lower() == "true"
    SMTP_REQUIRE_AUTH: bool = os.getenv("SMTP_REQUIRE_AUTH", "false").lower() == "true"
    
    # Email Settings
    DEFAULT_FROM_EMAIL: str = os.getenv("DEFAULT_FROM_EMAIL", "noreply@bakeryforecast.es")
    DEFAULT_FROM_NAME: str = os.getenv("DEFAULT_FROM_NAME", "Bakery Forecast")
    EMAIL_TEMPLATES_PATH: str = os.getenv("EMAIL_TEMPLATES_PATH", "/app/templates/email")
    
    # WhatsApp Business Cloud API Configuration (Meta/Facebook)
    WHATSAPP_ACCESS_TOKEN: str = os.getenv("WHATSAPP_ACCESS_TOKEN", "")
    WHATSAPP_PHONE_NUMBER_ID: str = os.getenv("WHATSAPP_PHONE_NUMBER_ID", "")
    WHATSAPP_BUSINESS_ACCOUNT_ID: str = os.getenv("WHATSAPP_BUSINESS_ACCOUNT_ID", "")
    WHATSAPP_API_VERSION: str = os.getenv("WHATSAPP_API_VERSION", "v18.0")
    WHATSAPP_WEBHOOK_VERIFY_TOKEN: str = os.getenv("WHATSAPP_WEBHOOK_VERIFY_TOKEN", "")
    WHATSAPP_TEMPLATES_PATH: str = os.getenv("WHATSAPP_TEMPLATES_PATH", "/app/templates/whatsapp")

    # Legacy Twilio Configuration (deprecated, for backward compatibility)
    WHATSAPP_API_KEY: str = os.getenv("WHATSAPP_API_KEY", "")  # Deprecated
    WHATSAPP_BASE_URL: str = os.getenv("WHATSAPP_BASE_URL", "https://api.twilio.com")  # Deprecated
    WHATSAPP_FROM_NUMBER: str = os.getenv("WHATSAPP_FROM_NUMBER", "")  # Deprecated
    
    # Notification Queuing
    MAX_RETRY_ATTEMPTS: int = int(os.getenv("MAX_RETRY_ATTEMPTS", "3"))
    RETRY_DELAY_SECONDS: int = int(os.getenv("RETRY_DELAY_SECONDS", "60"))
    BATCH_SIZE: int = int(os.getenv("NOTIFICATION_BATCH_SIZE", "100"))
    
    # Rate Limiting
    EMAIL_RATE_LIMIT_PER_HOUR: int = int(os.getenv("EMAIL_RATE_LIMIT_PER_HOUR", "1000"))
    WHATSAPP_RATE_LIMIT_PER_HOUR: int = int(os.getenv("WHATSAPP_RATE_LIMIT_PER_HOUR", "100"))
    
    # Spanish Localization
    DEFAULT_LANGUAGE: str = os.getenv("DEFAULT_LANGUAGE", "es")
    TIMEZONE: str = "Europe/Madrid"
    DATE_FORMAT: str = "%d/%m/%Y"
    TIME_FORMAT: str = "%H:%M"
    
    # Notification Types
    ENABLE_EMAIL_NOTIFICATIONS: bool = os.getenv("ENABLE_EMAIL_NOTIFICATIONS", "true").lower() == "true"
    ENABLE_WHATSAPP_NOTIFICATIONS: bool = os.getenv("ENABLE_WHATSAPP_NOTIFICATIONS", "true").lower() == "true"
    ENABLE_PUSH_NOTIFICATIONS: bool = os.getenv("ENABLE_PUSH_NOTIFICATIONS", "false").lower() == "true"
    
    # Template Categories
    ALERT_TEMPLATES_ENABLED: bool = True
    MARKETING_TEMPLATES_ENABLED: bool = os.getenv("MARKETING_TEMPLATES_ENABLED", "false").lower() == "true"
    TRANSACTIONAL_TEMPLATES_ENABLED: bool = True
    
    # Delivery Configuration
    IMMEDIATE_DELIVERY: bool = os.getenv("IMMEDIATE_DELIVERY", "true").lower() == "true"
    SCHEDULED_DELIVERY_ENABLED: bool = os.getenv("SCHEDULED_DELIVERY_ENABLED", "true").lower() == "true"
    BULK_DELIVERY_ENABLED: bool = os.getenv("BULK_DELIVERY_ENABLED", "true").lower() == "true"
    
    # Analytics
    DELIVERY_TRACKING_ENABLED: bool = os.getenv("DELIVERY_TRACKING_ENABLED", "true").lower() == "true"
    OPEN_TRACKING_ENABLED: bool = os.getenv("OPEN_TRACKING_ENABLED", "true").lower() == "true"
    CLICK_TRACKING_ENABLED: bool = os.getenv("CLICK_TRACKING_ENABLED", "true").lower() == "true"

settings = NotificationSettings()