"""
Configuration settings for the Data Scraping Service
"""
from functools import lru_cache
from typing import Optional, List
from pydantic import BaseSettings, validator
import os


class Settings(BaseSettings):
    """Application settings"""
    
    # Application
    APP_NAME: str = "Data Scraping Service"
    APP_VERSION: str = "1.0.0"
    DEBUG: bool = False
    ENVIRONMENT: str = "development"
    
    # Server
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    
    # MongoDB
    MONGODB_URI: str = "mongodb://localhost:27017"
    MONGODB_HOST: str = "localhost"
    MONGODB_PORT: int = 27017
    MONGODB_DB: str = "datadb"
    MONGODB_USERNAME: Optional[str] = None
    MONGODB_PASSWORD: Optional[str] = None
    
    # Kafka
    KAFKA_BOOTSTRAP_SERVERS: str = "localhost:9092"
    KAFKA_TOPIC_PREFIX: str = "data_service"
    KAFKA_GROUP_ID: str = "data-service-group"
    
    # Redis
    REDIS_URL: str = "redis://localhost:6379"
    REDIS_PASSWORD: Optional[str] = None
    REDIS_DB: int = 0
    
    # CORS
    CORS_ORIGINS: List[str] = ["*"]
    
    # Rate Limiting
    RATE_LIMIT_PER_MINUTE: int = 60
    RATE_LIMIT_BURST: int = 10
    
    # Security
    SECRET_KEY: str = "your-secret-key-change-this-in-production"
    JWT_SECRET_KEY: str = "your-jwt-secret-key"
    JWT_ALGORITHM: str = "HS256"
    
    # Web Scraping Settings
    USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    REQUEST_TIMEOUT: int = 30
    MAX_RETRIES: int = 3
    DELAY_BETWEEN_REQUESTS: float = 1.0
    
    # Search API Keys
    GOOGLE_API_KEY: Optional[str] = None
    GOOGLE_CSE_ID: Optional[str] = None
    BING_API_KEY: Optional[str] = None
    SERPAPI_KEY: Optional[str] = None
    
    # Selenium Settings
    SELENIUM_HEADLESS: bool = True
    SELENIUM_TIMEOUT: int = 30
    CHROME_DRIVER_PATH: Optional[str] = None
    
    # Data Processing
    MAX_ARTICLES_PER_SEARCH: int = 100
    ARTICLE_CONTENT_MIN_LENGTH: int = 100
    DUPLICATE_THRESHOLD: float = 0.8
    
    # File Storage
    UPLOAD_PATH: str = "/app/uploads"
    MAX_FILE_SIZE: int = 10485760  # 10MB
    
    # Monitoring
    PROMETHEUS_PORT: int = 9090
    LOG_LEVEL: str = "INFO"
    
    class Config:
        env_file = ".env"
        case_sensitive = True


class DevelopmentConfig(Settings):
    """Development configuration"""
    DEBUG: bool = True
    ENVIRONMENT: str = "development"
    LOG_LEVEL: str = "DEBUG"
    SELENIUM_HEADLESS: bool = False


class ProductionConfig(Settings):
    """Production configuration"""
    DEBUG: bool = False
    ENVIRONMENT: str = "production"
    LOG_LEVEL: str = "WARNING"
    SELENIUM_HEADLESS: bool = True


class TestingConfig(Settings):
    """Testing configuration"""
    DEBUG: bool = True
    ENVIRONMENT: str = "testing"
    MONGODB_DB: str = "test_datadb"
    LOG_LEVEL: str = "DEBUG"


@lru_cache()
def get_settings() -> Settings:
    """Get application settings based on environment"""
    environment = os.getenv("ENVIRONMENT", "development").lower()
    
    if environment == "production":
        return ProductionConfig()
    elif environment == "testing":
        return TestingConfig()
    else:
        return DevelopmentConfig()


# Create a global config instance
Config = get_settings