from sqlalchemy import Column, Integer, String, DateTime, Text, Boolean, ForeignKey, func
from app.database import Base


class EqpChecklist(Base):
    """设备巡检模板"""
    __tablename__ = "eqp_checklist"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    equipment_code = Column(String(30), nullable=True, index=True)
    equipment_name = Column(String(100), nullable=True)
    period = Column(String(20), default="daily")  # daily/weekly/monthly
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())


class EqpChecklistItem(Base):
    """巡检模板项目"""
    __tablename__ = "eqp_checklist_item"
    id = Column(Integer, primary_key=True, index=True)
    checklist_id = Column(Integer, ForeignKey("eqp_checklist.id"), nullable=False, index=True)
    item_name = Column(String(100), nullable=False)
    item_standard = Column(String(200), nullable=True)
    sort_order = Column(Integer, default=0)
    created_at = Column(DateTime, server_default=func.now())


class EqpInspectionRecord(Base):
    """巡检记录"""
    __tablename__ = "eqp_inspection_record"
    id = Column(Integer, primary_key=True, index=True)
    checklist_id = Column(Integer, ForeignKey("eqp_checklist.id"), nullable=False, index=True)
    checklist_name = Column(String(100), nullable=True)
    inspector = Column(String(50), nullable=False)
    inspect_time = Column(DateTime, nullable=False)
    status = Column(String(20), default="ok")  # ok/issue/urgent
    overall_result = Column(String(20), nullable=True)  # pass/fail
    remark = Column(Text, nullable=True)
    created_at = Column(DateTime, server_default=func.now())


class EqpInspectionDetail(Base):
    """巡检记录明细"""
    __tablename__ = "eqp_inspection_detail"
    id = Column(Integer, primary_key=True, index=True)
    record_id = Column(Integer, ForeignKey("eqp_inspection_record.id"), nullable=False, index=True)
    checklist_item_id = Column(Integer, nullable=True)
    item_name = Column(String(100), nullable=False)
    actual_value = Column(String(100), nullable=True)
    is_ok = Column(Boolean, default=True)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime, server_default=func.now())
