"""Telegram channel domain model — Pydantic v2 with ORM compatibility.

- :class:`TelegramChannel` — stores channel metadata with a direct FK
  to ``telegram_accounts``.  Each channel belongs to exactly one account.
"""

from __future__ import annotations

import uuid
from datetime import datetime
from typing import Any

from pydantic import field_validator

from repo_core.base.models import DomainModel


class TelegramChannel(DomainModel):
    """Telegram channel entity — stored in the ``telegram_channels`` table.

    Each row represents a unique Telegram channel that has been discovered
    or explicitly added by an admin.  The ``account_id`` FK points to the
    single ``telegram_accounts`` row that monitors this channel.
    """

    id: uuid.UUID
    channel_id: int
    title: str
    username: str | None = None
    kind: str = "channel"
    member_count: int | None = None
    profile_photo_base64: str | None = None
    description: str | None = None
    linked_chat_id: int | None = None
    admins_count: int | None = None
    online_count: int | None = None
    slowmode_seconds: int | None = None
    can_view_participants: bool | None = None
    verified: bool | None = None
    restricted: bool | None = None
    scam: bool | None = None
    fake: bool | None = None
    broadcast: bool | None = None
    megagroup: bool | None = None
    avg_views_last_10: float | None = None
    avg_reactions_last_10: float | None = None
    avg_comments_last_10: float | None = None
    total_posts_analyzed: int = 0
    backfill_threshold: int = 30
    """Minimum gap size in messages to trigger backfill.

    Gaps smaller than this count are ignored by the gap scanner.
    Default 30 means only gaps of 30+ missing messages get backfilled.
    """
    saved_preview: dict[str, Any] | None = None
    saved_preview_at: datetime | None = None
    account_id: uuid.UUID | None = None
    assigned_at: datetime | None = None
    assigned_by: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None

    @field_validator('total_posts_analyzed', mode='before')
    @classmethod
    def _coerce_total_posts_analyzed(cls, v: object) -> int:
        """Coerce NULL (from DB) to 0 so model construction never fails."""
        if v is None:
            return 0
        return int(v)  # type: ignore[arg-type]
