"""Prometheus metrics for deployment mode monitoring."""

from prometheus_client import Counter, Gauge

# Configuration viewer metrics
CONFIG_VIEW_REQUESTS = Counter(
    "mcp_config_view_requests_total",
    "Total configuration view requests",
    ["user_type"],
)

CONFIG_EXPORT_REQUESTS = Counter(
    "mcp_config_export_requests_total",
    "Total configuration export requests",
    ["format", "includes_sensitive"],
)

# Deployment mode info gauge
DEPLOYMENT_MODE_INFO = Gauge(
    "registry_deployment_mode_info",
    "Current deployment mode configuration",
    ["deployment_mode", "registry_mode"],
)

# Counter for skipped nginx updates
NGINX_UPDATES_SKIPPED = Counter(
    "registry_nginx_updates_skipped_total",
    "Number of nginx updates skipped due to registry-only mode",
    ["operation"],  # generate_config, reload
)

# Counter for nginx config file writes (issue #1044)
# status="success" when atomic write completes; status="failure" when temp file
# creation, write, or os.replace raises. Lets operators detect disk-full,
# permission, or replace failures (page on rate-of-change > 0).
NGINX_CONFIG_WRITES = Counter(
    "nginx_config_writes_total",
    "Total nginx config file writes performed by the registry, by outcome.",
    ["status"],  # success, failure
)

# Counter for blocked requests due to registry mode
MODE_BLOCKED_REQUESTS = Counter(
    "registry_mode_blocked_requests_total",
    "Requests blocked due to registry mode restrictions",
    ["path_category", "mode"],  # servers, agents, skills, federation
)

# Peer federation metrics (issue #561)
PEER_SYNC_FAILURES = Counter(
    "peer_sync_failures_total",
    "Total peer sync failures by failure type",
    ["peer_id", "failure_type"],  # auth_error, network_error, etc.
)

PEER_TOKEN_MISSING = Gauge(
    "peer_token_missing_total",
    "Number of peers missing federation tokens",
)

PEER_SYNC_DURATION_SECONDS = Gauge(
    "peer_sync_duration_seconds", "Duration of peer sync operations", ["peer_id", "success"]
)

# Application log handler metrics (issue #886)
APP_LOG_FLUSH_FAILURES = Counter(
    "app_log_mongodb_flush_failures_total",
    "Total MongoDB log handler flush failures",
    ["service"],
)

# Telemetry metrics (issue #558)
telemetry_sends_total = Counter(
    "telemetry_sends_total",
    "Total telemetry events sent",
    ["event", "status"],  # event: startup/heartbeat, status: success/timeout/error/2xx/4xx/5xx
)

# M2M orphan cleanup metrics (PR #942 follow-up)
M2M_ORPHAN_CLEANUPS_TOTAL = Counter(
    "m2m_orphan_cleanups_total",
    "Total M2M orphan cleanup deletions from MongoDB idp_m2m_clients",
    ["idp_had_record"],  # "true" when IdP also had the record, "false" for MongoDB-only orphans
)

# Cloud-detection metrics (issue #986)
CLOUD_DETECTION_TOTAL = Counter(
    "mcp_registry_cloud_detection_total",
    "Cloud-detection outcomes, labeled by cloud and detection method",
    ["cloud", "method"],  # cloud: aws/gcp/azure/unknown ; method: env/dmi/ecs_meta/k8s_heuristic/imds/unknown
)
