from app import db
from datetime import datetime
from app.core.data_parse.time_utils import get_east_asia_time_naive

class ParseTaskRepository(db.Model):
    __tablename__ = 'parse_task_repository'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    task_name = db.Column(db.String(100), nullable=False)
    task_status = db.Column(db.String(10), nullable=False)
    task_type = db.Column(db.String(50), nullable=False)
    task_source = db.Column(db.JSON, nullable=False)  # Changed to JSON to match jsonb
    collection_count = db.Column(db.Integer, default=0, nullable=False)
    parse_count = db.Column(db.Integer, default=0, nullable=False)
    parse_result = db.Column(db.JSON)  # Changed to JSON to match jsonb
    created_at = db.Column(db.DateTime, default=get_east_asia_time_naive, nullable=False)
    created_by = db.Column(db.String(50), nullable=False)
    updated_at = db.Column(db.DateTime, default=get_east_asia_time_naive, onupdate=get_east_asia_time_naive, nullable=False)
    updated_by = db.Column(db.String(50), nullable=False)
    
    def to_dict(self):
        return {
            'id': self.id,
            'task_name': self.task_name,
            'task_status': self.task_status,
            'task_type': self.task_type,
            'task_source': self.task_source,
            'collection_count': self.collection_count,
            'parse_count': self.parse_count,
            'parse_result': self.parse_result,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'created_by': self.created_by,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None,
            'updated_by': self.updated_by
        } 