"""This module defines the SQL data model for todo items."""

from sqlalchemy import Column, ForeignKey, Integer, String, DateTime, Boolean
from sqlalchemy.sql.functions import now
from sqlalchemy.orm import relationship

from todo.database.engine import Base
from todo.models.users import User


class TodoItem(Base):
    """Model for the todo_items table."""

    __tablename__ = "todo_items"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    title = Column('title', String, nullable=False, index=True)
    description = Column('description', String, nullable=False)
    done = Column('done', Boolean, nullable=False, default=False, index=True)

    created = Column('created', DateTime(timezone=True), nullable=False, server_default=now())
    updated = Column('updated', DateTime(timezone=True), nullable=False, server_default=now(), onupdate=now(), index=True)
    finished = Column('finished', DateTime(timezone=True), nullable=True, default=None)

    user_id = Column('user_id', Integer, ForeignKey('users.id', ondelete="CASCADE"), nullable=False, index=True)
    user = relationship(User, back_populates="todo_items", uselist=False)
