"""
Application Configuration
"""
from pydantic_settings import BaseSettings
from typing import List
import os


class Settings(BaseSettings):
    """Application settings loaded from environment variables"""

    # Application
    APP_NAME: str = "AI Chatbot Service"
    DEBUG: bool = True
    SECRET_KEY: str = "your-secret-key-change-in-production"

    # Database
    DATABASE_URL: str = "postgresql+asyncpg://postgres:password@localhost:5432/ai_chatbot"

    # Redis
    REDIS_URL: str = "redis://localhost:6379"

    # AI Services
    OPENAI_API_KEY: str = ""
    ANTHROPIC_API_KEY: str = ""
    GEMINI_API_KEY: str = ""
    GEMINI_MODEL: str = "gemini-3.8-flash"
    DEFAULT_AI_MODEL: str = "gpt-4o-mini"  # Cost-effective default

    # Ollama (Local AI - No API Key Required!)
    OLLAMA_URL: str = "http://ollama:11434"
    OLLAMA_DEFAULT_MODEL: str = "llama3.2:3b"  # Fast, efficient local model

    # WhatsApp Business API
    WHATSAPP_TOKEN: str = ""
    WHATSAPP_PHONE_NUMBER_ID: str = ""
    WHATSAPP_VERIFY_TOKEN: str = "ai-chatbot-verify"

    # CORS (comma-separated string from env, converted to list)
    CORS_ORIGINS: str = "http://localhost:3000,http://localhost:8080"

    @property
    def cors_origins_list(self) -> List[str]:
        """Convert comma-separated CORS_ORIGINS to list"""
        return [origin.strip() for origin in self.CORS_ORIGINS.split(",")]

    # Rate Limiting
    RATE_LIMIT_PER_MINUTE: int = 60
    RATE_LIMIT_PER_HOUR: int = 1000
    RATE_LIMIT_BURST: int = 10

    # Token Limits
    MAX_TOKENS_PER_MESSAGE: int = 1000
    MAX_CONVERSATION_HISTORY: int = 20

    # OAuth - Google
    GOOGLE_CLIENT_ID: str = ""
    GOOGLE_CLIENT_SECRET: str = ""
    GOOGLE_REDIRECT_URI: str = "http://localhost:8000/api/v1/auth/google/callback"

    # OAuth - Microsoft
    MICROSOFT_CLIENT_ID: str = ""
    MICROSOFT_CLIENT_SECRET: str = ""
    MICROSOFT_TENANT_ID: str = "common"
    MICROSOFT_REDIRECT_URI: str = "http://localhost:8000/api/v1/auth/microsoft/callback"

    # Frontend URL (for OAuth redirects)
    FRONTEND_URL: str = "http://localhost:3000"

    # AWS S3 Storage
    AWS_ACCESS_KEY_ID: str = ""
    AWS_SECRET_ACCESS_KEY: str = ""
    AWS_REGION: str = "us-east-1"
    S3_BUCKET_NAME: str = "ai-chatbot-uploads"
    S3_ENDPOINT_URL: str = ""  # For S3-compatible storage (MinIO, DigitalOcean Spaces)

    # Cloudinary (for image optimization)
    CLOUDINARY_CLOUD_NAME: str = ""
    CLOUDINARY_API_KEY: str = ""
    CLOUDINARY_API_SECRET: str = ""

    # File Upload Settings
    MAX_FILE_SIZE: int = 10 * 1024 * 1024  # 10MB
    MAX_IMAGE_SIZE: int = 5 * 1024 * 1024  # 5MB
    DEFAULT_STORAGE_PROVIDER: str = "s3"  # s3, cloudinary, local
    LOCAL_STORAGE_PATH: str = "./uploads"

    # Email Service
    EMAIL_PROVIDER: str = "smtp"  # sendgrid, ses, smtp

    # SendGrid
    SENDGRID_API_KEY: str = ""
    SENDGRID_SANDBOX_MODE: bool = False

    # AWS SES (uses AWS credentials above)
    AWS_SES_REGION: str = "us-east-1"
    SES_CONFIGURATION_SET: str = ""

    # SMTP (fallback)
    SMTP_HOST: str = "smtp.gmail.com"
    SMTP_PORT: int = 587
    SMTP_USER: str = ""
    SMTP_PASSWORD: str = ""
    SMTP_USE_TLS: bool = True

    # Email Sender
    FROM_EMAIL: str = "noreply@ai360.com"
    FROM_NAME: str = "AI360 Solutions"
    REPLY_TO_EMAIL: str = "support@ai360.com"

    # Celery Configuration
    CELERY_BROKER_URL: str = "redis://localhost:6379/0"
    CELERY_RESULT_BACKEND: str = "redis://localhost:6379/0"
    CELERY_TASK_SERIALIZER: str = "json"
    CELERY_RESULT_SERIALIZER: str = "json"
    CELERY_TIMEZONE: str = "Asia/Kolkata"

    # Worker Settings
    CELERY_WORKER_CONCURRENCY: int = 4
    CELERY_TASK_TIME_LIMIT: int = 3600  # 1 hour
    CELERY_TASK_SOFT_TIME_LIMIT: int = 3300  # 55 minutes

    # Invoice Settings
    INVOICE_DUE_DAYS: int = 15
    INVOICE_REMINDER_DAYS: int = 3
    TAX_PERCENTAGE: float = 18.0  # GST percentage

    # Usage Aggregation
    USAGE_RETENTION_DAYS: int = 365
    AUDIT_LOG_RETENTION_DAYS: int = 90
    WEBHOOK_LOG_RETENTION_DAYS: int = 30

    class Config:
        env_file = ".env"
        case_sensitive = True
        extra = "ignore"  # Ignore extra env variables


# Create settings instance
settings = Settings()
