"""Constants and configuration for heartbeat monitoring."""

from __future__ import annotations

from pathlib import Path

# Healthy messages are deleted after this many seconds
TTL_SECONDS = 15 * 60  # 15 minutes

# State file: stores [{message_id, sent_at}, ...] for pending deletions
STATE_DIR = Path.home() / ".cache" / "opendeviationbar" / "heartbeat"
STATE_FILE = STATE_DIR / "pending_messages.json"
STATHERA_STATE_FILE = STATE_DIR / "stathera_counts.json"

# Suppress duplicate alerts for 1 hour
ALERT_DEDUP_TTL = 3600

# Priority symbols shown first in the freshness section (matches kintsugi)
HEARTBEAT_PRIORITY_SYMBOLS = ("BTCUSDT", "ETHUSDT")

# Explicit set of result keys that gate the healthy/unhealthy decision.
# Only these keys participate — all other bool keys are informational.
# Adding a new health-gating check requires adding its key here.
HEALTH_GATE_KEYS = frozenset({
    "clickhouse",
    "sidecar",
    "kintsugi",
    "disk",
    "kintsugi_telemetry",
    "kintsugi_stuck",
    "kintsugi_failures",
    "kintsugi_memory",
    "stathera_overlaps",
    "stathera_gaps",
    "cross_midnight",
    "version_drift",
    "circuit_breaker_healthy",
    "seeder_ok",
})

# Known Stathera gap fingerprints — specific (symbol, threshold, first_agg_trade_id,
# prev_last_tid) combos where the root cause is definitively identified.
# When ALL active gaps match a known fingerprint, the Telegram alert shows a detailed
# diagnostic block instead of a generic 🔴 alert, so the reader immediately knows
# it's a known transient and no manual action is needed.
KNOWN_GAP_FINGERPRINTS: dict[tuple, dict] = {
    # ETHUSDT@500: REST pagination bug — fetch_aggtrades_rest() uses startTime+1ms when
    # advancing past a page boundary. Trade 1941397634 (T=1773422823391ms) was at the
    # same millisecond as page-end trade 1941397633, so the next page started at
    # T+1=1773422823392ms, silently skipping it.
    # Self-heals when Binance Vision publishes the day's archive (~24-48h from trade time).
    ("ETHUSDT", 500, 1941397635, 1941397633): {
        "cause": "pagination_bug_same_ms",
        "dropped_trade_id": 1941397634,
        "boundary_ts_ms": 1773422823391,
    },
}
