"""
资产调拨相关数据模型
"""
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 AssetTransferOrder(Base):
    """资产调拨单表"""

    __tablename__ = "asset_transfer_orders"

    id = Column(BigInteger, primary_key=True, index=True)
    order_code = Column(String(50), unique=True, nullable=False, index=True, comment="调拨单号")
    source_org_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=False, comment="调出网点ID")
    target_org_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=False, index=True, comment="调入网点ID")
    transfer_type = Column(String(20), nullable=False, index=True, comment="调拨类型(internal/external)")
    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)

    # 关系
    source_organization = relationship("Organization", foreign_keys=[source_org_id])
    target_organization = relationship("Organization", foreign_keys=[target_org_id])
    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("AssetTransferItem", back_populates="order", cascade="all, delete-orphan")

    # 索引
    __table_args__ = (
        Index("idx_transfer_orders_code", "order_code"),
        Index("idx_transfer_orders_source_org", "source_org_id"),
        Index("idx_transfer_orders_target_org", "target_org_id"),
    )

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


class AssetTransferItem(Base):
    """资产调拨单明细表"""

    __tablename__ = "asset_transfer_items"

    id = Column(BigInteger, primary_key=True, index=True)
    order_id = Column(BigInteger, ForeignKey("asset_transfer_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="资产编码")
    source_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=False, comment="调出网点ID")
    target_organization_id = Column(BigInteger, ForeignKey("organizations.id"), nullable=False, comment="调入网点ID")
    transfer_status = Column(String(20), default="pending", nullable=False, index=True, comment="调拨状态")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    # 关系
    order = relationship("AssetTransferOrder", back_populates="items")
    asset = relationship("Asset")
    source_organization = relationship("Organization", foreign_keys=[source_organization_id])
    target_organization = relationship("Organization", foreign_keys=[target_organization_id])

    # 索引
    __table_args__ = (
        Index("idx_transfer_items_order", "order_id"),
        Index("idx_transfer_items_asset", "asset_id"),
        Index("idx_transfer_items_status", "transfer_status"),
    )

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