"""
资产回收相关数据模型
"""
from datetime import datetime, date
from sqlalchemy import Column, BigInteger, String, Integer, Text, ForeignKey, DateTime, Date, Index
from sqlalchemy.orm import relationship
from app.db.base import Base


class AssetRecoveryOrder(Base):
    """资产回收单表"""

    __tablename__ = "asset_recovery_orders"

    id = Column(BigInteger, primary_key=True, index=True)
    order_code = Column(String(50), unique=True, nullable=False, index=True, comment="回收单号")
    recovery_type = Column(String(20), nullable=False, index=True, comment="回收类型(user=使用人回收/org=机构回收/scrap=报废回收)")
    title = Column(String(200), nullable=False, comment="标题")
    asset_count = Column(Integer, default=0, nullable=False, comment="资产数量")
    apply_user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, index=True, comment="申请人ID")
    apply_time = Column(DateTime, nullable=False, comment="申请时间")
    approval_status = Column(String(20), default="pending", nullable=False, index=True, comment="审批状态")
    approval_user_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="审批人ID")
    approval_time = Column(DateTime, nullable=True, comment="审批时间")
    approval_remark = Column(Text, nullable=True, comment="审批备注")
    execute_status = Column(String(20), default="pending", nullable=False, index=True, comment="执行状态")
    execute_user_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="执行人ID")
    execute_time = Column(DateTime, nullable=True, comment="执行时间")
    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)

    # 关系
    apply_user = relationship("User", foreign_keys=[apply_user_id])
    approval_user = relationship("User", foreign_keys=[approval_user_id])
    execute_user = relationship("User", foreign_keys=[execute_user_id])
    items = relationship("AssetRecoveryItem", back_populates="order", cascade="all, delete-orphan")

    # 索引
    __table_args__ = (
        Index("idx_recovery_orders_code", "order_code"),
        Index("idx_recovery_orders_type", "recovery_type"),
    )

    def __repr__(self):
        return f"<AssetRecoveryOrder(id={self.id}, order_code={self.order_code}, recovery_type={self.recovery_type})>"


class AssetRecoveryItem(Base):
    """资产回收单明细表"""

    __tablename__ = "asset_recovery_items"

    id = Column(BigInteger, primary_key=True, index=True)
    order_id = Column(BigInteger, ForeignKey("asset_recovery_orders.id", ondelete="CASCADE"), nullable=False, comment="回收单ID")
    asset_id = Column(BigInteger, ForeignKey("assets.id"), nullable=False, comment="资产ID")
    asset_code = Column(String(50), nullable=False, comment="资产编码")
    recovery_status = Column(String(20), default="pending", nullable=False, index=True, comment="回收状态")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    # 关系
    order = relationship("AssetRecoveryOrder", back_populates="items")
    asset = relationship("Asset")

    # 索引
    __table_args__ = (
        Index("idx_recovery_items_order", "order_id"),
        Index("idx_recovery_items_asset", "asset_id"),
        Index("idx_recovery_items_status", "recovery_status"),
    )

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