"""
任务相关Schema
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field


class TaskBase(BaseModel):
    """任务基础Schema"""
    title: str = Field(..., description="任务标题")
    description: Optional[str] = Field(None, description="任务描述")
    priority: str = Field("medium", description="优先级(low/medium/high)")
    deadline: Optional[datetime] = Field(None, description="截止时间")
    requirements: Optional[dict] = Field(None, description="任务要求配置")
    course_ids: List[int] = Field(default_factory=list, description="关联课程ID列表")
    user_ids: List[int] = Field(default_factory=list, description="分配用户ID列表")


class TaskCreate(TaskBase):
    """创建任务"""
    pass


class TaskUpdate(BaseModel):
    """更新任务"""
    title: Optional[str] = None
    description: Optional[str] = None
    priority: Optional[str] = None
    status: Optional[str] = None
    deadline: Optional[datetime] = None
    requirements: Optional[dict] = None
    progress: Optional[int] = None


class TaskResponse(BaseModel):
    """任务响应"""
    id: int
    title: str
    description: Optional[str]
    priority: str
    status: str
    creator_id: int
    deadline: Optional[datetime]
    requirements: Optional[dict]
    progress: int
    created_at: datetime
    updated_at: datetime
    # 扩展字段
    courses: List[str] = Field(default_factory=list, description="课程名称列表")
    assigned_count: int = Field(0, description="分配人数")
    completed_count: int = Field(0, description="完成人数")

    class Config:
        from_attributes = True


class TaskStatsResponse(BaseModel):
    """任务统计响应"""
    total: int = Field(0, description="总任务数")
    ongoing: int = Field(0, description="进行中")
    completed: int = Field(0, description="已完成")
    expired: int = Field(0, description="已过期")
    avg_completion_rate: float = Field(0.0, description="平均完成率")



