"""
批次執行日誌 Model
記錄所有批次作業的執行結果
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text

from app.db.base import Base


class BatchLog(Base):
    """批次執行日誌表"""

    __tablename__ = "tenant_batch_logs"

    id = Column(Integer, primary_key=True, index=True)
    batch_name = Column(String(100), nullable=False, index=True, comment="批次名稱")
    status = Column(String(20), nullable=False, comment="執行狀態: success/failed/warning")
    message = Column(Text, comment="執行訊息或錯誤詳情")
    started_at = Column(DateTime, nullable=False, default=datetime.utcnow, index=True, comment="開始時間")
    finished_at = Column(DateTime, comment="完成時間")
    duration_seconds = Column(Integer, comment="執行時間 (秒)")

    # 通用欄位
    is_active = Column(Boolean, default=True, nullable=False, comment="是否啟用")
    edit_by = Column(String(100), nullable=True, comment="最後編輯者")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="建立時間")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新時間")

    def __repr__(self):
        return f"<BatchLog {self.batch_name} [{self.status}] @ {self.started_at}>"
