from datetime import date
from typing import TYPE_CHECKING, Optional

from sqlalchemy import Date, Enum, ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.core.database import Base

if TYPE_CHECKING:
    from src.models.users import UsersORM

status_enum = Enum("open", "closed", "in_progress", "todo", name="status_enum")
priority_enum = Enum("low", "medium", "high", "critical", name="priority_enum")


class TasksORM(Base):
    __tablename__ = "tasks"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
    title: Mapped[str] = mapped_column(String(100))
    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
    status: Mapped[str] = mapped_column(status_enum, default="open")
    priority: Mapped[str] = mapped_column(priority_enum, default="medium")
    time_spent: Mapped[int] = mapped_column(default=0)

    user: Mapped["UsersORM"] = relationship(back_populates="tasks")
