"""
维修管理相关数据模型
"""
from datetime import datetime, date
from sqlalchemy import Column, BigInteger, String, Integer, Text, ForeignKey, DateTime, Date, Numeric, Index
from sqlalchemy.orm import relationship
from app.db.base import Base


class MaintenanceRecord(Base):
    """维修记录表"""

    __tablename__ = "maintenance_records"

    id = Column(BigInteger, primary_key=True, index=True)
    record_code = Column(String(50), unique=True, nullable=False, index=True, comment="维修单号")
    asset_id = Column(BigInteger, ForeignKey("assets.id"), nullable=False, index=True, comment="资产ID")
    asset_code = Column(String(50), nullable=False, comment="资产编码")
    fault_description = Column(Text, nullable=False, comment="故障描述")
    fault_type = Column(String(50), nullable=True, comment="故障类型")
    report_user_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="报修人ID")
    report_time = Column(DateTime, default=datetime.utcnow, nullable=False, index=True, comment="报修时间")
    priority = Column(String(20), default="normal", nullable=False, comment="优先级")
    maintenance_type = Column(String(20), nullable=True, comment="维修类型")
    vendor_id = Column(BigInteger, ForeignKey("suppliers.id"), nullable=True, comment="维修供应商ID")
    maintenance_cost = Column(Numeric(18, 2), nullable=True, comment="维修费用")
    start_time = Column(DateTime, nullable=True, comment="开始维修时间")
    complete_time = Column(DateTime, nullable=True, comment="完成维修时间")
    maintenance_user_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="维修人员ID")
    maintenance_result = Column(Text, nullable=True, comment="维修结果描述")
    replaced_parts = Column(Text, nullable=True, comment="更换的配件")
    status = Column(String(20), default="pending", nullable=False, index=True, comment="状态")
    images = Column(Text, nullable=True, comment="维修图片URL（多个逗号分隔）")
    remark = Column(Text, nullable=True, comment="备注")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    created_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)
    updated_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)

    # 关系
    asset = relationship("Asset")
    vendor = relationship("Supplier")
    report_user = relationship("User", foreign_keys=[report_user_id])
    maintenance_user = relationship("User", foreign_keys=[maintenance_user_id])
    created_user = relationship("User", foreign_keys=[created_by])
    updated_user = relationship("User", foreign_keys=[updated_by])

    # 索引
    __table_args__ = (
        Index("idx_maintenance_records_code", "record_code"),
        Index("idx_maintenance_records_asset", "asset_id"),
        Index("idx_maintenance_records_status", "status"),
        Index("idx_maintenance_records_time", "report_time"),
    )

    def __repr__(self):
        return f"<MaintenanceRecord(id={self.id}, record_code={self.record_code}, asset_code={self.asset_code})>"
