"""
ERP 物流域 ORM 模型
映射: erp_shipping_records / erp_shipping_items
"""

from __future__ import annotations

import uuid
from datetime import date, datetime

from sqlalchemy import (
    Boolean,
    Date,
    DateTime,
    ForeignKey,
    Numeric,
    String,
    Text,
    func,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.models.base import Base


class ErpShippingRecord(Base):
    __tablename__ = "erp_shipping_records"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    shipping_no: Mapped[str] = mapped_column(String(30), unique=True, nullable=False)
    order_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_orders.id"), nullable=False
    )
    carrier: Mapped[str | None] = mapped_column(String(100), nullable=True)
    tracking_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="transit")
    ship_date: Mapped[date] = mapped_column(Date, default=date.today)
    remark: Mapped[str | None] = mapped_column(Text, nullable=True)
    operator_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("sys_users.id"), nullable=True
    )
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now()
    )
    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)

    # 关系
    order: Mapped["ErpOrder"] = relationship("ErpOrder", lazy="selectin")  # noqa: F821
    operator: Mapped["SysUser | None"] = relationship("SysUser", lazy="selectin")  # noqa: F821
    items: Mapped[list[ErpShippingItem]] = relationship(
        "ErpShippingItem", back_populates="shipping_record", lazy="selectin"
    )


class ErpShippingItem(Base):
    __tablename__ = "erp_shipping_items"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    shipping_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_shipping_records.id"), nullable=False
    )
    order_item_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_order_items.id"), nullable=False
    )
    sku_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_product_skus.id"), nullable=False
    )
    shipped_qty: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now()
    )
    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)

    # 关系
    shipping_record: Mapped[ErpShippingRecord] = relationship(
        "ErpShippingRecord", back_populates="items"
    )
    order_item: Mapped["ErpOrderItem"] = relationship("ErpOrderItem", lazy="selectin")  # noqa: F821
    sku: Mapped["ProductSku"] = relationship("ProductSku", lazy="selectin")  # noqa: F821
