from fastapi import APIRouter

from app.api.deps import CurrentUser, DbSession
from app.repositories import savings as crud_savings
from app.schemas.order import OrderReportOut, OrderReportRequest

router = APIRouter(prefix="/api/v1/order", tags=["order"])


@router.post(
    "/report",
    response_model=OrderReportOut,
    summary="上报归因订单(比价后5分钟内点链接 + 支付金额与比价价相差≤1元)",
)
def report_order(req: OrderReportRequest, user: CurrentUser, db: DbSession) -> OrderReportOut:
    # 记账唯一真相表是 savings_record(source='compare')。
    rec, duplicated = crud_savings.create_from_report(db, user.id, req)
    return OrderReportOut(
        id=rec.id,
        platform=rec.platform or req.platform,
        pay_channel=rec.pay_channel or req.pay_channel,
        compared_price_cents=rec.compared_price_cents or 0,
        paid_amount_cents=rec.order_amount_cents,
        duplicated=duplicated,
    )
