"""Bat Genie - An Intelligent AI Assistant."""
import logging
import os
from pathlib import Path
from typing import Optional

from .context_manager import ContextManager
from .goal_manager import GoalManager
from .resource_manager import ResourceManager
from .learning_manager import LearningManager
from .intelligence_manager import IntelligenceManager, IntelligenceConfig

__version__ = "0.1.0"

def setup_logging(log_level: str = "INFO",
                 log_file: Optional[str] = None) -> None:
    """Set up logging configuration."""
    numeric_level = getattr(logging, log_level.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError(f"Invalid log level: {log_level}")
    
    # Create formatters
    console_formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )
    file_formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(pathname)s:%(lineno)d - %(message)s'
    )
    
    # Set up root logger
    root_logger = logging.getLogger()
    root_logger.setLevel(numeric_level)
    
    # Console handler
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(console_formatter)
    root_logger.addHandler(console_handler)
    
    # File handler if specified
    if log_file:
        log_path = Path(log_file)
        log_path.parent.mkdir(parents=True, exist_ok=True)
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(file_formatter)
        root_logger.addHandler(file_handler)

def initialize_bat_genie(workspace_root: str,
                        log_level: str = "INFO",
                        log_file: Optional[str] = None,
                        enable_auto_learning: bool = True,
                        enable_goal_tracking: bool = True,
                        enable_resource_caching: bool = True) -> IntelligenceManager:
    """Initialize Bat Genie with the specified configuration."""
    # Set up logging
    setup_logging(log_level, log_file)
    logger = logging.getLogger(__name__)
    logger.info(f"Initializing Bat Genie v{__version__}")
    
    # Create configuration
    config = IntelligenceConfig(
        workspace_root=workspace_root,
        cache_dir=os.path.join('.cursor', 'cache'),
        learning_db_path=os.path.join('.cursor', 'learning.json'),
        context_db_path=os.path.join('.cursor', 'context_db.json'),
        enable_auto_learning=enable_auto_learning,
        enable_goal_tracking=enable_goal_tracking,
        enable_resource_caching=enable_resource_caching
    )
    
    # Initialize intelligence manager
    intelligence_manager = IntelligenceManager(config)
    logger.info("Bat Genie initialization complete")
    
    return intelligence_manager

__all__ = [
    'ContextManager',
    'GoalManager',
    'ResourceManager',
    'LearningManager',
    'IntelligenceManager',
    'IntelligenceConfig',
    'initialize_bat_genie',
    'setup_logging',
    '__version__'
] 