"""FastAPI Monitor - Enhanced monitoring middleware with dashboard.

A production-ready monitoring solution for FastAPI applications with:
- Zero-configuration setup
- Real-time dashboard
- Comprehensive metrics
- Security-first design
- High performance
"""

import logging

# Import main components
from .config import ColorScheme, MonitorConfig, get_config

# Legacy imports for backward compatibility
from .dashboard import create_dashboard_app as create_dashboard_app_legacy
from .dashboard_v2 import create_dashboard_app
from .middleware import MonitorMiddleware as MonitorMiddlewareLegacy
from .middleware_v2 import MonitorMiddleware, create_monitor_middleware
from .models import (
    AnalyticsData,
    MonitorRecord,
    PaginationInfo,
    RequestData,
    ResponseData,
    StatsData,
)
from .repository import MonitorRepository, SQLiteRepository
from .services import MonitorService

__version__ = "2.0.0"
__author__ = "FastAPI Monitor Team"
__email__ = "support@fastapi-monitor.com"

# Public API
__all__ = [
    # Main components
    "MonitorMiddleware",
    "create_dashboard_app",
    "create_monitor_middleware",

    # Configuration
    "MonitorConfig",
    "ColorScheme",
    "get_config",

    # Models
    "MonitorRecord",
    "RequestData",
    "ResponseData",
    "StatsData",
    "AnalyticsData",
    "PaginationInfo",

    # Services and Repository
    "MonitorService",
    "MonitorRepository",
    "SQLiteRepository",

    # Legacy compatibility
    "create_dashboard_app_legacy",
    "MonitorMiddlewareLegacy",
]

# Setup logging
logging.getLogger(__name__).addHandler(logging.NullHandler())


def setup_logging(level: str = "INFO") -> None:
    """Setup logging for FastAPI Monitor.
    
    Args:
        level: Logging level (DEBUG, INFO, WARNING, ERROR)
    """
    logging.basicConfig(
        level=getattr(logging, level.upper()),
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )


# Quick setup functions for common use cases
def quick_setup(
    app,
    db_path: str = "monitor.db",
    dashboard_path: str = "/monitor",
    exclude_paths: list = None,
    username: str = None,
    password: str = None,
    color_scheme: ColorScheme = ColorScheme.DEFAULT
):
    """Quick setup for FastAPI Monitor with sensible defaults.
    
    Args:
        app: FastAPI application instance
        db_path: Path to SQLite database
        dashboard_path: Path to mount dashboard
        exclude_paths: Paths to exclude from monitoring
        username: Dashboard username (optional)
        password: Dashboard password (optional)
        color_scheme: Dashboard color scheme
    
    Example:
        ```python
        from fastapi import FastAPI
        from fastapi_monitor import quick_setup
        
        app = FastAPI()
        quick_setup(app)
        ```
    """
    # Create configuration
    config = MonitorConfig(
        db_path=db_path,
        exclude_paths=exclude_paths or [],
        dashboard_username=username,
        dashboard_password=password,
        color_scheme=color_scheme
    )

    # Add middleware
    middleware = MonitorMiddleware(app, config=config)
    app.add_middleware(type(middleware), **middleware.__dict__)

    # Mount dashboard
    dashboard_app = create_dashboard_app(config=config)
    app.mount(dashboard_path, dashboard_app)

    return app


def create_production_setup(
    app,
    db_path: str = "monitor.db",
    dashboard_path: str = "/monitor",
    username: str = None,
    password: str = None,
    max_records: int = 1000000,
    cleanup_interval: int = 3600,
    exclude_paths: list = None
):
    """Production-ready setup with optimized settings.
    
    Args:
        app: FastAPI application instance
        db_path: Path to SQLite database
        dashboard_path: Path to mount dashboard
        username: Dashboard username (required for production)
        password: Dashboard password (required for production)
        max_records: Maximum records to keep
        cleanup_interval: Cleanup interval in seconds
        exclude_paths: Paths to exclude from monitoring
    
    Example:
        ```python
        from fastapi import FastAPI
        from fastapi_monitor import create_production_setup
        
        app = FastAPI()
        create_production_setup(
            app,
            username="admin",
            password="secure-password-123"
        )
        ```
    """
    if not username or not password:
        raise ValueError("Username and password are required for production setup")

    config = MonitorConfig(
        db_path=db_path,
        dashboard_username=username,
        dashboard_password=password,
        max_records=max_records,
        cleanup_interval=cleanup_interval,
        exclude_paths=exclude_paths or [dashboard_path, "/health", "/metrics"],
        log_level="WARNING"  # Reduce log noise in production
    )

    # Add middleware
    middleware = MonitorMiddleware(app, config=config)
    app.add_middleware(type(middleware), **middleware.__dict__)

    # Mount dashboard
    dashboard_app = create_dashboard_app(config=config)
    app.mount(dashboard_path, dashboard_app)

    return app
