# shared/config/rabbitmq_config.py
"""
RabbitMQ configuration for the alert and recommendation system
Supports both alerts and recommendations through a unified topic exchange
"""

RABBITMQ_CONFIG = {
    "exchanges": {
        "alerts": {
            "name": "alerts.exchange",
            "type": "topic",
            "durable": True,
            "auto_delete": False
        },
        "dead_letter": {
            "name": "dlx.exchange",
            "type": "direct",
            "durable": True,
            "auto_delete": False
        }
    },
    "queues": {
        "alert_processing": {
            "name": "alert.processing.queue",
            "durable": True,
            "arguments": {
                "x-message-ttl": 3600000,  # 1 hour TTL
                "x-max-length": 10000,      # Max 10k messages
                "x-overflow": "reject-publish",  
                "x-dead-letter-exchange": "dlx.exchange",
                "x-dead-letter-routing-key": "failed.items"
            }
        },
        "dead_letter": {
            "name": "alert.dead_letter.queue",
            "durable": True,
            "arguments": {
                "x-message-ttl": 86400000  # 24 hours for dead letters
            }
        }
    },
    "bindings": [
        {
            "queue": "alert.processing.queue",
            "exchange": "alerts.exchange",
            "routing_key": "*.*.*"  # alert/recommendation.severity.service
        },
        {
            "queue": "alert.dead_letter.queue",
            "exchange": "dlx.exchange",
            "routing_key": "failed.items"
        }
    ],
    "routing_patterns": {
        # alert/recommendation.severity.service_name
        "alert": "alert.{severity}.{service}",
        "recommendation": "recommendation.{severity}.{service}",
        "all_alerts": "alert.*.*",
        "all_recommendations": "recommendation.*.*",
        "urgent_items": "*.urgent.*",
        "high_items": "*.high.*"
    }
}

def get_routing_key(item_type: str, severity: str, service: str) -> str:
    """Generate routing key for item publishing"""
    return f"{item_type}.{severity}.{service}"

def get_binding_patterns(item_types: list = None, severities: list = None, services: list = None) -> list:
    """Generate binding patterns for selective consumption"""
    patterns = []
    
    item_types = item_types or ["alert", "recommendation"]
    severities = severities or ["urgent", "high", "medium", "low"]
    services = services or ["*"]
    
    for item_type in item_types:
        for severity in severities:
            for service in services:
                patterns.append(f"{item_type}.{severity}.{service}")
    
    return patterns