"""
任务相关模型
"""
from datetime import datetime
from typing import List, Optional
from sqlalchemy import Column, Integer, String, Text, DateTime, Enum as SQLEnum, JSON, Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import BaseModel
from enum import Enum


class TaskPriority(str, Enum):
    """任务优先级"""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"


class TaskStatus(str, Enum):
    """任务状态"""
    PENDING = "pending"  # 待开始
    ONGOING = "ongoing"  # 进行中
    COMPLETED = "completed"  # 已完成
    EXPIRED = "expired"  # 已过期


class AssignmentStatus(str, Enum):
    """分配状态"""
    NOT_STARTED = "not_started"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"


class Task(BaseModel):
    """任务表"""
    __tablename__ = "tasks"

    title: Mapped[str] = mapped_column(String(200), nullable=False, comment="任务标题")
    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True, comment="任务描述")
    priority: Mapped[TaskPriority] = mapped_column(
        SQLEnum(TaskPriority, values_callable=lambda x: [e.value for e in x]),
        default=TaskPriority.MEDIUM,
        nullable=False,
        comment="优先级"
    )
    status: Mapped[TaskStatus] = mapped_column(
        SQLEnum(TaskStatus, values_callable=lambda x: [e.value for e in x]),
        default=TaskStatus.PENDING,
        nullable=False,
        comment="任务状态"
    )
    creator_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False, comment="创建人ID")
    deadline: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, comment="截止时间")
    requirements: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, comment="任务要求配置")
    progress: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="完成进度")
    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)

    # 关系
    creator = relationship("User", backref="created_tasks", foreign_keys=[creator_id])
    course_links = relationship("TaskCourse", back_populates="task", cascade="all, delete-orphan")
    assignments = relationship("TaskAssignment", back_populates="task", cascade="all, delete-orphan")


class TaskCourse(BaseModel):
    """任务课程关联表"""
    __tablename__ = "task_courses"

    task_id: Mapped[int] = mapped_column(Integer, ForeignKey("tasks.id"), nullable=False, comment="任务ID")
    course_id: Mapped[int] = mapped_column(Integer, ForeignKey("courses.id"), nullable=False, comment="课程ID")

    # 关系
    task = relationship("Task", back_populates="course_links")
    course = relationship("Course")


class TaskAssignment(BaseModel):
    """任务分配表"""
    __tablename__ = "task_assignments"

    task_id: Mapped[int] = mapped_column(Integer, ForeignKey("tasks.id"), nullable=False, comment="任务ID")
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False, comment="分配用户ID")
    team_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, comment="团队ID")
    status: Mapped[AssignmentStatus] = mapped_column(
        SQLEnum(AssignmentStatus, values_callable=lambda x: [e.value for e in x]),
        default=AssignmentStatus.NOT_STARTED,
        nullable=False,
        comment="完成状态"
    )
    progress: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="个人完成进度")
    completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, comment="完成时间")

    # 关系
    task = relationship("Task", back_populates="assignments")
    user = relationship("User")


__all__ = ["Task", "TaskCourse", "TaskAssignment", "TaskPriority", "TaskStatus", "AssignmentStatus"]



