# -*- coding: utf-8 -*-
"""充值记录任务"""

import json

from .base_task import BaseTask, TaskContext
from loaders.facts.topup import TopupLoader
from models.parsers import TypeParser


class TopupsTask(BaseTask):
    """同步储值充值结算记录"""

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

    def extract(self, context: TaskContext) -> dict:
        params = self._merge_common_params(
            {
                "siteId": context.store_id,
                "rangeStartTime": TypeParser.format_timestamp(context.window_start, self.tz),
                "rangeEndTime": TypeParser.format_timestamp(context.window_end, self.tz),
            }
        )
        records, _ = self.api.get_paginated(
            endpoint="/Site/GetRechargeSettleList",
            params=params,
            page_size=self.config.get("api.page_size", 200),
            data_path=("data",),
            list_key="settleList",
        )
        return {"records": records}

    def transform(self, extracted: dict, context: TaskContext) -> dict:
        parsed, skipped = [], 0
        for raw in extracted.get("records", []):
            mapped = self._parse_topup(raw, context.store_id)
            if mapped:
                parsed.append(mapped)
            else:
                skipped += 1
        return {
            "records": parsed,
            "fetched": len(extracted.get("records", [])),
            "skipped": skipped,
        }

    def load(self, transformed: dict, context: TaskContext) -> dict:
        loader = TopupLoader(self.db)
        inserted, updated, loader_skipped = loader.upsert_topups(transformed["records"])
        return {
            "fetched": transformed["fetched"],
            "inserted": inserted,
            "updated": updated,
            "skipped": transformed["skipped"] + loader_skipped,
            "errors": 0,
        }

    def _parse_topup(self, raw: dict, store_id: int) -> dict | None:
        node = raw.get("settleList") if isinstance(raw.get("settleList"), dict) else raw
        topup_id = TypeParser.parse_int(node.get("id"))
        if not topup_id:
            self.logger.warning("跳过缺少充值ID的记录: %s", raw)
            return None

        return {
            "store_id": store_id,
            "topup_id": topup_id,
            "member_id": TypeParser.parse_int(node.get("memberId")),
            "member_name": node.get("memberName"),
            "member_phone": node.get("memberPhone"),
            "card_id": TypeParser.parse_int(node.get("tenantMemberCardId")),
            "card_type_name": node.get("memberCardTypeName"),
            "pay_amount": TypeParser.parse_decimal(node.get("payAmount")),
            "consume_money": TypeParser.parse_decimal(node.get("consumeMoney")),
            "settle_status": node.get("settleStatus"),
            "settle_type": node.get("settleType"),
            "settle_name": node.get("settleName"),
            "settle_relate_id": TypeParser.parse_int(node.get("settleRelateId")),
            "pay_time": TypeParser.parse_timestamp(
                node.get("payTime") or node.get("pay_time"), self.tz
            ),
            "create_time": TypeParser.parse_timestamp(
                node.get("createTime") or node.get("create_time"), self.tz
            ),
            "operator_id": TypeParser.parse_int(node.get("operatorId")),
            "operator_name": node.get("operatorName"),
            "payment_method": node.get("paymentMethod"),
            "refund_amount": TypeParser.parse_decimal(node.get("refundAmount")),
            "cash_amount": TypeParser.parse_decimal(node.get("cashAmount")),
            "card_amount": TypeParser.parse_decimal(node.get("cardAmount")),
            "balance_amount": TypeParser.parse_decimal(node.get("balanceAmount")),
            "online_amount": TypeParser.parse_decimal(node.get("onlineAmount")),
            "rounding_amount": TypeParser.parse_decimal(node.get("roundingAmount")),
            "adjust_amount": TypeParser.parse_decimal(node.get("adjustAmount")),
            "goods_money": TypeParser.parse_decimal(node.get("goodsMoney")),
            "table_charge_money": TypeParser.parse_decimal(node.get("tableChargeMoney")),
            "service_money": TypeParser.parse_decimal(node.get("serviceMoney")),
            "coupon_amount": TypeParser.parse_decimal(node.get("couponAmount")),
            "order_remark": node.get("orderRemark"),
            "raw_data": json.dumps(raw, ensure_ascii=False),
        }
