"""
资产分配相关数据模型
"""
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 AssetAllocationOrder(Base):
    """资产分配单表"""

    __tablename__ = "asset_allocation_orders"

    id = Column(BigInteger, primary_key=True, index=True)
    order_code = Column(String(50), unique=True, nullable=False, index=True, comment="分配单号")
    order_type = Column(String(20), nullable=False, index=True, comment="单据类型")
    title = Column(String(200), nullable=False, comment="标题")
    source_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=True, comment="调出网点ID")
    target_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=False, index=True, comment="调入网点ID")
    applicant_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, index=True, comment="申请人ID")
    approver_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="审批人ID")
    approval_status = Column(String(20), default="pending", nullable=False, index=True, comment="审批状态")
    approval_time = Column(DateTime, nullable=True, comment="审批时间")
    approval_remark = Column(Text, nullable=True, comment="审批备注")
    expect_execute_date = Column(Date, nullable=True, comment="预计执行日期")
    actual_execute_date = Column(Date, nullable=True, comment="实际执行日期")
    executor_id = Column(BigInteger, ForeignKey("users.id"), nullable=True, comment="执行人ID")
    execute_status = Column(String(20), default="pending", nullable=False, 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)
    created_by = Column(BigInteger, ForeignKey("users.id"), nullable=False)
    updated_by = Column(BigInteger, ForeignKey("users.id"), nullable=True)

    # 关系
    source_organization = relationship("Organization", foreign_keys=[source_organization_id])
    target_organization = relationship("Organization", foreign_keys=[target_organization_id])
    applicant = relationship("User", foreign_keys=[applicant_id])
    approver = relationship("User", foreign_keys=[approver_id])
    executor = relationship("User", foreign_keys=[executor_id])
    items = relationship("AssetAllocationItem", back_populates="order", cascade="all, delete-orphan")

    # 索引
    __table_args__ = (
        Index("idx_allocation_orders_code", "order_code"),
        Index("idx_allocation_orders_target_org", "target_organization_id"),
    )

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


class AssetAllocationItem(Base):
    """资产分配单明细表"""

    __tablename__ = "asset_allocation_items"

    id = Column(BigInteger, primary_key=True, index=True)
    order_id = Column(BigInteger, ForeignKey("asset_allocation_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="资产编码")
    asset_name = Column(String(200), nullable=False, comment="资产名称")
    from_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=True, comment="原网点ID")
    to_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=True, comment="目标网点ID")
    from_status = Column(String(20), nullable=True, comment="原状态")
    to_status = Column(String(20), nullable=True, comment="目标状态")
    execute_status = Column(String(20), default="pending", nullable=False, index=True, comment="执行状态")
    execute_time = Column(DateTime, nullable=True, comment="执行时间")
    failure_reason = Column(Text, 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)

    # 关系
    order = relationship("AssetAllocationOrder", back_populates="items")
    asset = relationship("Asset")
    from_organization = relationship("Organization", foreign_keys=[from_organization_id])
    to_organization = relationship("Organization", foreign_keys=[to_organization_id])

    # 索引
    __table_args__ = (
        Index("idx_allocation_items_order", "order_id"),
        Index("idx_allocation_items_asset", "asset_id"),
        Index("idx_allocation_items_status", "execute_status"),
    )

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