"""Business-rule validators for Telegram messages."""
from __future__ import annotations

from typing import Any

from repo_core.base.validators import BaseValidator, ValidationError, ValidatorPipeline


class TelegramMessageTextValidator(BaseValidator):
    """Ensure the message text is present and within Telegram's length limits."""

    def __init__(self, max_length: int = 4096) -> None:
        self._max = max_length

    async def validate(self, data: str) -> str:
        value = data.strip() if isinstance(data, str) else data
        if not value:
            raise ValidationError("Message text cannot be blank.", field="message")
        if len(value) > self._max:
            raise ValidationError(
                f"Message text must be at most {self._max} characters.",
                field="message",
            )
        return value


class TelegramPeerIdValidator(BaseValidator):
    """Ensure the peer_id is a positive integer."""

    async def validate(self, data: int) -> int:
        if not isinstance(data, int):
            raise ValidationError(
                "peer_id must be an integer.", field="peer_id"
            )
        if data <= 0:
            raise ValidationError(
                "peer_id must be a positive integer.", field="peer_id"
            )
        return data


def build_telegram_message_pipeline() -> ValidatorPipeline:
    """Return a ready-to-use pipeline that validates message text."""
    return ValidatorPipeline(
        TelegramMessageTextValidator(),
    )
