"""CPS 对账订单(cps_order)。

从美团联盟 query_order 按时间窗拉回、按 sid 归群的订单明细。字段对齐 query_order
实测返回:
  - payPrice / profit 是「元」字符串 → 入库统一转「分」(与全站口径一致)
  - payTime / updateTime 是秒级时间戳 → 入库转 tz-aware datetime
  - status: 2付款 3完成 4取消 5风控 6结算(取消/风控不计佣金)
order_id 全局唯一,reconcile 按它 upsert(订单状态会变,重复拉则更新)。
"""
from __future__ import annotations

from datetime import datetime

from sqlalchemy import JSON, DateTime, Integer, String, func
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class CpsOrder(Base):
    __tablename__ = "cps_order"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    # 美团订单号(加密串),全局唯一,upsert 幂等键。
    order_id: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
    # 渠道追踪位 = 群 sid(历史无 sid 订单为空)。按它归群聚合。
    sid: Mapped[str | None] = mapped_column(String(64), index=True, nullable=True)
    act_id: Mapped[str | None] = mapped_column(String(64), index=True, nullable=True)
    biz_line: Mapped[int | None] = mapped_column(Integer, nullable=True)  # 1=外卖
    trade_type: Mapped[int | None] = mapped_column(Integer, nullable=True)  # 1=cps 2=cpa

    pay_price_cents: Mapped[int | None] = mapped_column(Integer, nullable=True)
    commission_cents: Mapped[int | None] = mapped_column(Integer, nullable=True)  # 预估佣金(profit)
    commission_rate: Mapped[str | None] = mapped_column(String(16), nullable=True)  # "300"=3% "10"=0.1%
    refund_price_cents: Mapped[int | None] = mapped_column(Integer, nullable=True)
    refund_profit_cents: Mapped[int | None] = mapped_column(Integer, nullable=True)

    # 美团订单状态: 2付款 3完成 4取消 5风控 6结算
    mt_status: Mapped[str | None] = mapped_column(String(8), index=True, nullable=True)
    invalid_reason: Mapped[str | None] = mapped_column(String(128), nullable=True)
    product_name: Mapped[str | None] = mapped_column(String(512), nullable=True)

    pay_time: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), index=True, nullable=True
    )
    mt_update_time: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)

    raw: Mapped[dict] = mapped_column(
        JSON().with_variant(JSONB(), "postgresql"), nullable=False, default=dict
    )
    first_seen: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
    )

    def __repr__(self) -> str:  # pragma: no cover
        return (
            f"<CpsOrder id={self.id} order_id={self.order_id!r} "
            f"sid={self.sid!r} status={self.mt_status} profit_cents={self.commission_cents}>"
        )
