"""
站内消息通知相关的数据验证模型
"""
from typing import Optional, List
from datetime import datetime
from enum import Enum

from pydantic import BaseModel, Field, ConfigDict


class NotificationType(str, Enum):
    """通知类型枚举"""
    POSITION_ASSIGN = "position_assign"  # 岗位分配
    COURSE_ASSIGN = "course_assign"      # 课程分配
    EXAM_REMIND = "exam_remind"          # 考试提醒
    TASK_ASSIGN = "task_assign"          # 任务分配
    SYSTEM = "system"                    # 系统通知


class NotificationBase(BaseModel):
    """
    通知基础模型
    """
    title: str = Field(..., min_length=1, max_length=200, description="通知标题")
    content: Optional[str] = Field(None, description="通知内容")
    type: NotificationType = Field(default=NotificationType.SYSTEM, description="通知类型")
    related_id: Optional[int] = Field(None, description="关联数据ID")
    related_type: Optional[str] = Field(None, max_length=50, description="关联数据类型")


class NotificationCreate(NotificationBase):
    """
    创建通知模型
    """
    user_id: int = Field(..., description="接收用户ID")
    sender_id: Optional[int] = Field(None, description="发送者用户ID")


class NotificationBatchCreate(BaseModel):
    """
    批量创建通知模型（发送给多个用户）
    """
    user_ids: List[int] = Field(..., min_length=1, description="接收用户ID列表")
    title: str = Field(..., min_length=1, max_length=200, description="通知标题")
    content: Optional[str] = Field(None, description="通知内容")
    type: NotificationType = Field(default=NotificationType.SYSTEM, description="通知类型")
    related_id: Optional[int] = Field(None, description="关联数据ID")
    related_type: Optional[str] = Field(None, max_length=50, description="关联数据类型")
    sender_id: Optional[int] = Field(None, description="发送者用户ID")


class NotificationUpdate(BaseModel):
    """
    更新通知模型
    """
    is_read: Optional[bool] = Field(None, description="是否已读")


class NotificationInDB(NotificationBase):
    """
    数据库中的通知模型
    """
    model_config = ConfigDict(from_attributes=True)
    
    id: int
    user_id: int
    is_read: bool
    sender_id: Optional[int] = None
    created_at: datetime
    updated_at: datetime


class NotificationResponse(NotificationInDB):
    """
    通知响应模型（可扩展发送者信息）
    """
    sender_name: Optional[str] = Field(None, description="发送者姓名")


class NotificationListResponse(BaseModel):
    """
    通知列表响应模型
    """
    items: List[NotificationResponse]
    total: int
    unread_count: int


class NotificationCountResponse(BaseModel):
    """
    未读通知数量响应模型
    """
    unread_count: int
    total: int


class MarkReadRequest(BaseModel):
    """
    标记已读请求模型
    """
    notification_ids: Optional[List[int]] = Field(None, description="通知ID列表，为空则标记全部已读")

