"""
할일관리 시스템 스키마
"""
from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime
from uuid import UUID


class TodoCommentBase(BaseModel):
    content: str = Field(..., min_length=1, max_length=1000)


class TodoCommentCreate(TodoCommentBase):
    pass


class TodoCommentUpdate(BaseModel):
    content: Optional[str] = Field(None, min_length=1, max_length=1000)


class TodoCommentResponse(TodoCommentBase):
    id: UUID
    todo_item_id: UUID
    user_id: UUID
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class TodoItemBase(BaseModel):
    content: str = Field(..., min_length=1, max_length=2000)


class TodoItemCreate(TodoItemBase):
    """초기 할일 생성 (draft 상태)"""
    photo_url: Optional[str] = Field(None, max_length=500, description="첨부 사진 URL")


class TodoItemSchedule(BaseModel):
    """할일 일정 설정"""
    start_date: datetime
    estimated_minutes: int = Field(..., ge=1, le=120)  # 1분~2시간


class TodoItemUpdate(BaseModel):
    """할일 수정"""
    content: Optional[str] = Field(None, min_length=1, max_length=2000)
    photo_url: Optional[str] = Field(None, max_length=500, description="첨부 사진 URL")
    status: Optional[str] = Field(None, pattern="^(draft|scheduled|active|completed|delayed)$")
    start_date: Optional[datetime] = None
    estimated_minutes: Optional[int] = Field(None, ge=1, le=120)
    delayed_until: Optional[datetime] = None


class TodoItemDelay(BaseModel):
    """할일 지연"""
    delayed_until: datetime


class TodoItemSplit(BaseModel):
    """할일 분할"""
    subtasks: List[str] = Field(..., min_items=2, max_items=10)
    estimated_minutes_per_task: List[int] = Field(..., min_items=2, max_items=10)


class TodoItemResponse(TodoItemBase):
    id: UUID
    user_id: UUID
    photo_url: Optional[str] = None
    status: str
    created_at: datetime
    start_date: Optional[datetime]
    estimated_minutes: Optional[int]
    completed_at: Optional[datetime]
    delayed_until: Optional[datetime]
    parent_id: Optional[UUID]
    split_order: Optional[int]
    
    # 댓글 수
    comment_count: int = 0
    
    class Config:
        from_attributes = True


class TodoItemWithComments(TodoItemResponse):
    """댓글이 포함된 할일 응답"""
    comments: List[TodoCommentResponse] = []


class TodoStats(BaseModel):
    """할일 통계"""
    total_count: int
    draft_count: int
    scheduled_count: int
    active_count: int
    completed_count: int
    delayed_count: int
    completion_rate: float  # 완료율 (%)


class TodoDashboard(BaseModel):
    """할일 대시보드"""
    stats: TodoStats
    today_todos: List[TodoItemResponse]
    overdue_todos: List[TodoItemResponse]
    upcoming_todos: List[TodoItemResponse]
