"""
Configuration validators for CBCFacil
"""
import logging
from typing import List, Dict


class ConfigurationError(Exception):
    """Raised when configuration is invalid"""
    pass


def validate_environment() -> List[str]:
    """
    Validate required environment variables and configuration.
    Returns a list of warnings/errors.
    """
    from .settings import settings

    warnings = []
    errors = []

    # Check critical configurations
    if not settings.has_webdav_config:
        warnings.append("WebDAV credentials not configured - file sync will not work")

    if not settings.has_ai_config:
        warnings.append("No AI providers configured - summary generation will not work")

    # Validate API keys format if provided
    if settings.ZAI_AUTH_TOKEN:
        if len(settings.ZAI_AUTH_TOKEN) < 10:
            errors.append("ZAI_AUTH_TOKEN appears to be invalid (too short)")

    if settings.GEMINI_API_KEY:
        if len(settings.GEMINI_API_KEY) < 20:
            errors.append("GEMINI_API_KEY appears to be invalid (too short)")

    # Validate dashboard secret
    if not settings.DASHBOARD_SECRET_KEY:
        warnings.append("DASHBOARD_SECRET_KEY not set - using default is not recommended for production")

    if settings.DASHBOARD_SECRET_KEY == "dashboard-secret-key-change-in-production":
        warnings.append("Using default dashboard secret key - please change in production")

    # Check CUDA availability
    try:
        import torch
        if not torch.cuda.is_available():
            warnings.append("CUDA not available - GPU acceleration will be disabled")
    except ImportError:
        warnings.append("PyTorch not installed - GPU acceleration will be disabled")

    # Print warnings
    for warning in warnings:
        logging.warning(f"Configuration warning: {warning}")

    # Raise error if critical issues
    if errors:
        error_msg = "Configuration errors:\n" + "\n".join(f"- {e}" for e in errors)
        logging.error(error_msg)
        raise ConfigurationError(error_msg)

    return warnings
