"""
ERP 交易域 ORM 模型
映射: erp_orders / erp_order_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 ErpOrder(Base):
    __tablename__ = "erp_orders"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    order_no: Mapped[str] = mapped_column(String(30), unique=True, nullable=False)
    customer_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("crm_customers.id"), nullable=False
    )
    salesperson_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("sys_users.id"), nullable=True
    )
    total_amount: Mapped[float] = mapped_column(Numeric(14, 2), default=0)
    shipping_state: Mapped[str] = mapped_column(
        String(20), nullable=False, default="pending"
    )
    payment_state: Mapped[str] = mapped_column(
        String(20), nullable=False, default="unpaid"
    )
    paid_amount: Mapped[float] = mapped_column(Numeric(14, 2), default=0)
    remark: Mapped[str | None] = mapped_column(Text, nullable=True)
    order_date: Mapped[date] = mapped_column(Date, default=date.today)
    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)

    # 关系
    customer: Mapped["CrmCustomer"] = relationship("CrmCustomer", lazy="selectin")  # noqa: F821
    salesperson: Mapped["SysUser | None"] = relationship("SysUser", lazy="selectin")  # noqa: F821
    items: Mapped[list[ErpOrderItem]] = relationship(
        "ErpOrderItem", back_populates="order", lazy="selectin"
    )


class ErpOrderItem(Base):
    __tablename__ = "erp_order_items"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    order_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_orders.id"), nullable=False
    )
    sku_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("erp_product_skus.id"), nullable=False
    )
    qty: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
    unit_price: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
    sub_total: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False)
    shipped_qty: Mapped[float] = mapped_column(Numeric(12, 2), default=0)
    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", back_populates="items")
    sku: Mapped["ProductSku"] = relationship("ProductSku", lazy="selectin")  # noqa: F821
