# -*- coding: utf-8 -*-
"""支付记录ETL任务"""
import json

from .base_task import BaseTask, TaskContext
from loaders.facts.payment import PaymentLoader
from models.parsers import TypeParser


class PaymentsTask(BaseTask):
    """支付记录 E/T/L 任务"""

    def get_task_code(self) -> str:
        return "PAYMENTS"

    # ------------------------------------------------------------------ E/T/L hooks
    def extract(self, context: TaskContext) -> dict:
        """调用 API 抓取支付记录"""
        params = self._merge_common_params(
            {
                "siteId": context.store_id,
                "StartPayTime": TypeParser.format_timestamp(context.window_start, self.tz),
                "EndPayTime": TypeParser.format_timestamp(context.window_end, self.tz),
            }
        )
        records, pages_meta = self.api.get_paginated(
            endpoint="/PayLog/GetPayLogListPage",
            params=params,
            page_size=self.config.get("api.page_size", 200),
            data_path=("data",),
        )
        return {"records": records, "meta": pages_meta}

    def transform(self, extracted: dict, context: TaskContext) -> dict:
        """解析支付 JSON"""
        parsed, skipped = [], 0
        for rec in extracted.get("records", []):
            cleaned = self._parse_payment(rec, context.store_id)
            if cleaned:
                parsed.append(cleaned)
            else:
                skipped += 1
        return {
            "records": parsed,
            "fetched": len(extracted.get("records", [])),
            "skipped": skipped,
        }

    def load(self, transformed: dict, context: TaskContext) -> dict:
        """写入 fact_payment"""
        loader = PaymentLoader(self.db)
        inserted, updated, loader_skipped = loader.upsert_payments(
            transformed["records"], context.store_id
        )
        counts = {
            "fetched": transformed["fetched"],
            "inserted": inserted,
            "updated": updated,
            "skipped": transformed["skipped"] + loader_skipped,
            "errors": 0,
        }
        return counts

    # ------------------------------------------------------------------ helpers
    def _parse_payment(self, raw: dict, store_id: int) -> dict | None:
        """解析支付记录"""
        try:
            return {
                "store_id": store_id,
                "pay_id": TypeParser.parse_int(raw.get("payId") or raw.get("id")),
                "order_id": TypeParser.parse_int(raw.get("orderId")),
                "order_settle_id": TypeParser.parse_int(
                    raw.get("orderSettleId") or raw.get("order_settle_id")
                ),
                "order_trade_no": TypeParser.parse_int(
                    raw.get("orderTradeNo") or raw.get("order_trade_no")
                ),
                "relate_type": raw.get("relateType") or raw.get("relate_type"),
                "relate_id": TypeParser.parse_int(raw.get("relateId") or raw.get("relate_id")),
                "site_id": TypeParser.parse_int(
                    raw.get("siteId") or raw.get("site_id") or store_id
                ),
                "tenant_id": TypeParser.parse_int(raw.get("tenantId") or raw.get("tenant_id")),
                "pay_time": TypeParser.parse_timestamp(raw.get("payTime"), self.tz),
                "create_time": TypeParser.parse_timestamp(
                    raw.get("createTime") or raw.get("create_time"), self.tz
                ),
                "pay_amount": TypeParser.parse_decimal(raw.get("payAmount")),
                "fee_amount": TypeParser.parse_decimal(
                    raw.get("feeAmount")
                    or raw.get("serviceFee")
                    or raw.get("channelFee")
                    or raw.get("fee_amount")
                ),
                "discount_amount": TypeParser.parse_decimal(
                    raw.get("discountAmount")
                    or raw.get("couponAmount")
                    or raw.get("discount_amount")
                ),
                "pay_type": raw.get("payType"),
                "payment_method": raw.get("paymentMethod") or raw.get("payment_method"),
                "online_pay_channel": raw.get("onlinePayChannel")
                or raw.get("online_pay_channel"),
                "pay_status": raw.get("payStatus"),
                "pay_terminal": raw.get("payTerminal") or raw.get("pay_terminal"),
                "remark": raw.get("remark"),
                "raw_data": json.dumps(raw, ensure_ascii=False),
            }
        except Exception as exc:
            self.logger.warning("解析支付记录失败: %s, 原始数据: %s", exc, raw)
            return None
