"""Telegram crawl metric — per-message operational telemetry (warehouse-only).

Stores attribution and timing data for each crawled Telegram message.
One row per message, keyed by ``(peer_id, message_id)`` — the natural
Telegram business key.

This is a **warehouse-only** model — no PostgreSQL or Elasticsearch
backing.  All dashboard queries run against Doris.
"""

from __future__ import annotations

import uuid
from datetime import datetime

from repo_core.base.models import DomainModel


class TelegramCrawlMetric(DomainModel):
    """Per-message operational metadata for dashboard metrics.

    Keyed by ``(peer_id, message_id)`` — the natural Telegram business
    key.  ``peer_id`` is the flattened integer peer identifier (channel_id,
    chat_id, or user_id).  ``message_id`` is Telegram's native message ID
    (the ``telegram_id`` field from the raw ``TelegramMessage``).

    Status lifecycle::

        ingested → persisted → parsed → analyzed
                       ↘ error

    Usage::

        metric = TelegramCrawlMetric(
            peer_id=-1001234567890,
            message_id=123456789,
            account_id=uuid.uuid4(),
            proxy_id="d-17432776848",
            message_sent_at=1718534400,
            status="ingested",
            ingested_at=datetime.now(timezone.utc),
        )
        row = metric.to_warehouse()
    """

    peer_id: int
    message_id: int
    account_id: uuid.UUID | None = None
    proxy_id: str | None = None
    message_sent_at: datetime
    status: str
    ingested_at: datetime
    persisted_at: datetime | None = None
    parsed_at: datetime | None = None
    parse_duration: int | None = None
    analyzed_at: datetime | None = None
    analyze_duration: int | None = None
    created_at: datetime | None = None

    # Warehouse-only — no DB or search backends
    _warehouse_include: set[str] | None = {
        "peer_id",
        "message_id",
        "account_id",
        "proxy_id",
        "message_sent_at",
        "status",
        "ingested_at",
        "persisted_at",
        "parsed_at",
        "parse_duration",
        "analyzed_at",
        "analyze_duration",
        "created_at",
    }

    _db_include: set[str] | None = set()
    _search_include: set[str] | None = set()
    _timestamps: set[str] = {"created_at"}
