from flask_sqlalchemy import SQLAlchemy
from app import db  # 从 app/__init__.py 导入 db 实例

class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(120), nullable=False)
    description = db.Column(db.String(255), nullable=True)
    done = db.Column(db.Boolean, default=False)

    def to_dict(self):
        """将模型实例转换为字典，方便序列化为 JSON"""
        return {
            'id': self.id,
            'title': self.title,
            'description': self.description,
            'done': self.done
        }

    def __repr__(self):
        return f'<Task {self.id}: {self.title}>'
