from __future__ import annotations

from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict, Field, field_validator


def _normalize_text(value: Any) -> str:
    return str(value or "").strip()


class NotificationStatePatch(BaseModel):
    notification_id: str = Field(min_length=1, max_length=180)
    read: bool = False
    hidden: bool = False
    context_json: dict[str, Any] = Field(default_factory=dict)

    @field_validator("notification_id", mode="before")
    @classmethod
    def normalize_notification_id(cls, value: Any) -> str:
        return _normalize_text(value)

    @field_validator("context_json", mode="before")
    @classmethod
    def normalize_context(cls, value: Any) -> dict[str, Any]:
        return value if isinstance(value, dict) else {}


class NotificationStateBatchPatch(BaseModel):
    states: list[NotificationStatePatch] = Field(default_factory=list, max_length=500)


class NotificationStateRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    notification_id: str
    read_at: datetime | None
    hidden_at: datetime | None
    context_json: dict[str, Any]
    updated_at: datetime


class NotificationStateListRead(BaseModel):
    states: list[NotificationStateRead] = Field(default_factory=list)
