"""Alert hooks for Publishing Integrity system.

Provides a stub alert service that logs to structured output.
Can be extended to integrate with Slack, email, or other notification systems.
"""

import json
import logging
from datetime import datetime, timezone
from typing import Dict, Any, Optional

log = logging.getLogger("pub-integrity")

# Alert severity thresholds
ALERT_THRESHOLDS = {
    "identity_collision": "high",
    "team_mismatch": "high",
    "props_suppressed": "medium",
    "freshness_stale": "medium",
    "regression_failure": "critical",
    "official_roster_mismatch": "high",
    "player_wrong_team": "critical",
    "published_without_verification": "high",
}


def trigger_alert(
    alert_type: str,
    data: Optional[Dict[str, Any]] = None,
    severity: Optional[str] = None,
):
    """
    Trigger an alert. Currently logs structured output.
    Extend this to send to Slack, email, or dashboards.
    """
    sev = severity or ALERT_THRESHOLDS.get(alert_type, "medium")

    alert_payload = {
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "type": alert_type,
        "severity": sev,
        "data": data or {},
    }

    if sev == "critical":
        log.critical(f"[ALERT] {alert_type}: {json.dumps(data or {}, default=str)}")
    elif sev == "high":
        log.warning(f"[ALERT] {alert_type}: {json.dumps(data or {}, default=str)}")
    else:
        log.info(f"[ALERT] {alert_type}: {json.dumps(data or {}, default=str)}")

    # Stub: future integration point for external alerting
    # _send_to_slack(alert_payload)
    # _send_to_email(alert_payload)
