"""
Round Processor - Dispatches round data processing based on data_source_type

Responsibilities:
- Act as the unified entry point for round data processing
- Determine data source type (leetify vs classic)
- Dispatch to appropriate specialized processors
- Coordinate economy, event, and spatial processors
"""

import sqlite3
import logging

logger = logging.getLogger(__name__)


class RoundProcessor:
    @staticmethod
    def process(match_data, conn: sqlite3.Connection) -> bool:
        """
        Process round data by dispatching to specialized processors
        
        Args:
            match_data: MatchData object containing parsed JSON
            conn: L2 database connection
            
        Returns:
            bool: True if successful
        """
        try:
            # Import specialized processors
            from . import economy_processor
            from . import event_processor
            from . import spatial_processor
            
            if match_data.data_source_type == 'leetify':
                logger.debug(f"Processing leetify data for match {match_data.match_id}")
                # Process leetify rounds
                success = economy_processor.EconomyProcessor.process_leetify(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process leetify economy for match {match_data.match_id}")
                
                # Process leetify events
                success = event_processor.EventProcessor.process_leetify_events(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process leetify events for match {match_data.match_id}")
                    
            elif match_data.data_source_type == 'classic':
                logger.debug(f"Processing classic data for match {match_data.match_id}")
                # Process classic rounds (basic round info)
                success = _process_classic_rounds(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process classic rounds for match {match_data.match_id}")
                
                # Process classic economy (NEW)
                success = economy_processor.EconomyProcessor.process_classic(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process classic economy for match {match_data.match_id}")
                
                # Process classic events (kills, bombs)
                success = event_processor.EventProcessor.process_classic_events(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process classic events for match {match_data.match_id}")
                
                # Process spatial data (xyz coordinates)
                success = spatial_processor.SpatialProcessor.process(match_data, conn)
                if not success:
                    logger.warning(f"Failed to process spatial data for match {match_data.match_id}")
                    
            else:
                logger.info(f"No round data to process for match {match_data.match_id} (data_source_type={match_data.data_source_type})")
            
            return True
            
        except Exception as e:
            logger.error(f"Error in round processor for match {match_data.match_id}: {e}")
            import traceback
            traceback.print_exc()
            return False


def _process_classic_rounds(match_data, conn: sqlite3.Connection) -> bool:
    """
    Process basic round information for classic data source
    
    Classic round data contains:
    - current_score (ct/t scores, type, pasttime, final_round_time)
    - But lacks economy data
    """
    try:
        # This is handled by event_processor for classic
        # Classic rounds are extracted from round_list structure
        # which is processed in event_processor.process_classic_events
        return True
    except Exception as e:
        logger.error(f"Error processing classic rounds: {e}")
        return False
