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


class DowntimeCategory(Base):
    """停机类别"""
    __tablename__ = "downtime_categories"

    id = Column(Integer, primary_key=True, index=True)
    code = Column(String(20), unique=True, nullable=False)
    name = Column(String(50), nullable=False)
    category = Column(String(20), comment="大类: equipment/process/material/other")
    description = Column(Text)
    is_active = Column(Integer, default=1)


class DowntimeRecord(Base):
    """停机记录"""
    __tablename__ = "downtime_records"

    id = Column(Integer, primary_key=True, index=True)
    category_id = Column(Integer, ForeignKey("downtime_categories.id"))
    category_code = Column(String(20), index=True)
    category_name = Column(String(50))
    shift = Column(String(10), comment="班次")
    shift_date = Column(DateTime, comment="班期")
    start_time = Column(DateTime, nullable=False, comment="停机开始时间")
    end_time = Column(DateTime, comment="停机结束时间")
    duration = Column(Float, comment="停机时长min（自动计算）")
    equipment_code = Column(String(30), comment="停机设备编号")
    fault_desc = Column(Text, comment="故障描述")
    action_taken = Column(Text, comment="处理措施")
    root_cause = Column(Text, comment="根本原因")
    reporter = Column(String(50), comment="报告人")
    handler = Column(String(50), comment="处理人")
    is_planned = Column(Integer, default=0, comment="是否计划停机")
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
