from datetime import UTC, datetime
from typing import Any, cast

from sqlalchemy import JSON, Column, DateTime
from sqlmodel import Field, SQLModel


def utcnow() -> datetime:
    return datetime.now(UTC).replace(tzinfo=None)


class Notification(SQLModel, table=True):
    __tablename__ = cast(Any, "notification")

    id: str = Field(primary_key=True, max_length=36)
    restaurant_id: str = Field(foreign_key="restaurant.id", nullable=False)
    type: str = Field(nullable=False, max_length=50)
    title: str = Field(nullable=False, max_length=255)
    body: str | None = Field(default=None)
    is_read: bool = Field(default=False, nullable=False)
    metadata_json: dict[str, Any] | None = Field(
        default=None, sa_column=Column(JSON, nullable=True)
    )
    created_at: datetime = Field(
        default_factory=utcnow,
        sa_column=Column(DateTime(), nullable=False, default=utcnow),
    )


class NotificationRead(SQLModel):
    id: str
    restaurant_id: str
    type: str
    title: str
    body: str | None
    is_read: bool
    metadata_json: dict[str, Any] | None
    created_at: datetime


class UnreadCountResponse(SQLModel):
    count: int
