"""Business-rule validators for the Category domain."""
from __future__ import annotations

from repo_core.base.validators import BaseValidator, ValidationError


class CategoryTitleValidator(BaseValidator):
    """Reject blank or overly-long category titles."""

    def __init__(self, max_length: int = 100) -> 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("Title cannot be blank.", field="title")
        if len(value) > self._max:
            raise ValidationError(
                f"Title must be at most {self._max} characters.", field="title"
            )
        return value


class CategoryNameValidator(BaseValidator):
    """Reject blank or overly-long category names."""

    def __init__(self, max_length: int = 100) -> 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("Name cannot be blank.", field="name")
        if len(value) > self._max:
            raise ValidationError(
                f"Name must be at most {self._max} characters.", field="name"
            )
        return value
