# ============================================================================
# SIMPLE MONITORING SCRIPT
# ============================================================================

"""
Simple script to monitor captured errors
Save as: monitor_errors.py
"""

def monitor_errors_simple():
    """Monitor errors from Kafka"""
    import json
    from kafka import KafkaConsumer
    from loguru import logger
    
    logger.info("=" * 80)
    logger.info("MONITORING CAPTURED ERRORS")
    logger.info("=" * 80)
    logger.info("Press Ctrl+C to stop\n")
    
    try:
        consumer = KafkaConsumer(
            'rca.events',
            bootstrap_servers=['localhost:9092'],
            auto_offset_reset='earliest',
            group_id='error-monitor',
            value_deserializer=lambda x: json.loads(x.decode('utf-8'))
        )
        
        logger.success("✓ Connected to Kafka")
        logger.info("Waiting for events...\n")
        
        count = 0
        for message in consumer:
            count += 1
            event = message.value
            
            logger.info(f"\n{'='*60}")
            logger.info(f"EVENT #{count}")
            logger.info(f"{'='*60}")
            logger.info(f"ID:        {event.get('id')}")
            logger.info(f"Time:      {event.get('timestamp_iso')}")
            logger.info(f"Component: {event.get('component')}")
            logger.info(f"Severity:  {event.get('severity')}")
            logger.info(f"Type:      {event.get('error_type')}")
            logger.info(f"Message:   {event.get('message')}")
            
            if event.get('metadata'):
                logger.info(f"Metadata:  {event.get('metadata')}")
            
    except KeyboardInterrupt:
        logger.info("\n\n✓ Monitoring stopped")
    except Exception as e:
        logger.error(f"✗ Error: {e}")


if __name__ == "__main__":
    monitor_errors_simple()