"""Telegram crawl metrics and crawler events payload models.

Payloads for ``telegram_message_metrics_ready`` and ``telegram_message_events_ready``
Kafka topics.  Validated by consumers before writing to Doris.
"""

from __future__ import annotations

import uuid
from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict


class TelegramCrawlMetricPayload(BaseModel):
    """Payload published for each crawled Telegram message.

    Keyed by ``(peer_id, message_id)`` — the natural Telegram business key.
    Covers attribution (peer_id), timing (durations),
    and status tracking through the pipeline.
    """

    model_config = ConfigDict(from_attributes=True, populate_by_name=True)

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


class CrawlerEventPayload(BaseModel):
    """Payload published by tele-crawler for lifecycle events.

    Validated by the persistence worker before writing to Doris.
    """

    model_config = ConfigDict(from_attributes=True, populate_by_name=True)

    id: uuid.UUID
    account_id: uuid.UUID | None = None
    proxy_id: str | None = None
    crawler_instance_id: str
    event_type: str
    event_data: dict[str, Any] | None = None
    created_at: str | None = None  # ISO 8601 timestamp
