"""
Main application setup for the Congressional MCP server.
Handles FastMCP server creation, routing, and middleware configuration.
"""

# Load environment configuration first
from congress_api.core.api_config import ENV

import logging
import sys
import os
import re

# Add the parent directory to sys.path to enable absolute imports
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

# Import the MCP server and feature initializer
from congress_api.mcp_server import mcp, initialize_mcp_features

# Import prompts

class SensitiveInfoFilter(logging.Filter):
    """Filter to redact sensitive information from logs."""

    def __init__(self, patterns=None):
        super().__init__()
        self.patterns = patterns or [
            (r'api_key=([^&\s]+)', 'api_key=REDACTED'),
            (r'"api_key_configured":\s*true', '"api_key_configured": [REDACTED]'),
            (r'"base_url":\s*"[^"]+"', '"base_url": "[REDACTED]"')
        ]

    def filter(self, record):
        if isinstance(record.msg, str):
            for pattern, replacement in self.patterns:
                record.msg = re.sub(pattern, replacement, record.msg)
        return True

def setup_logging():
    """Configure logging for the application based on environment with security considerations."""

    # Determine log level based on environment
    if ENV == "production":
        default_level = logging.INFO
    elif ENV == "staging":
        default_level = logging.INFO
    else:  # development
        default_level = logging.DEBUG

    # Allow override via environment variable
    log_level_name = os.getenv("LOG_LEVEL", "")
    if log_level_name:
        try:
            default_level = getattr(logging, log_level_name.upper())
        except AttributeError:
            print(f"Invalid LOG_LEVEL: {log_level_name}. Using default.", file=sys.stderr)

    # Configure logging format - JSON in production for better parsing
    if ENV == "production":
        # Simple JSON-like format for production without file paths and line numbers
        log_format = '{"time": "%(asctime)s", "service": "congress-mcp", "level": "%(levelname)s", "message": "%(message)s"}'
    else:
        # More readable format for development and staging, still without exposing file paths
        log_format = "%(asctime)s - %(levelname)s - %(message)s"

    # Create a filter for sensitive information
    sensitive_filter = SensitiveInfoFilter()

    # Configure handler with filter
    handler = logging.StreamHandler(sys.stderr)
    handler.setFormatter(logging.Formatter(log_format))
    handler.addFilter(sensitive_filter)

    # Configure root logger
    root_logger = logging.getLogger()
    root_logger.setLevel(default_level)
    root_logger.handlers = [handler]  # Replace any existing handlers

    # Set specific logger levels
    loggers_config = {
        'congress_api': default_level,
        'congress_api.features': logging.INFO if ENV == "production" else default_level,
        'httpx': logging.WARNING,
        'uvicorn': logging.INFO,
    }

    for logger_name, level in loggers_config.items():
        logging.getLogger(logger_name).setLevel(level)

    # Get logger for this module
    logger = logging.getLogger(__name__)

    # Log minimal startup information in production
    if ENV == "production":
        logger.info("Application starting in production mode")
    else:
        logger.info(f"Application starting in {ENV} mode with log level {logging.getLevelName(default_level)}")

    return logger

def main():
    """Main entry point for the Congress.gov API MCP server."""
    logger = setup_logging()
    logger.info("Starting Congress.gov API MCP server")

    # Log server health at startup
    from congress_api.core.api_config import get_api_config
    config = get_api_config()

    # Log minimal server health information
    if ENV == "production":
        # In production, only log that the server is configured
        logger.info("Server configuration loaded")
    else:
        # In development/staging, log minimal configuration details
        # The sensitive info filter will redact sensitive values
        logger.info(f"API key configured: {config['api_key_configured']}")
        logger.info(f"Caching enabled: {config['caching_enabled']}")

    # Initialize all MCP features (tools, resources, prompts)
    initialize_mcp_features()

    logger.info("Server is ready")
    return mcp

# This is the object that MCP will look for
server = main()
