# -*- coding: utf-8 -*-
"""退款记录任务"""

import json

from .base_task import BaseTask, TaskContext
from loaders.facts.refund import RefundLoader
from models.parsers import TypeParser


class RefundsTask(BaseTask):
    """同步支付退款流水"""

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

    def extract(self, context: TaskContext) -> dict:
        params = self._merge_common_params(
            {
                "siteId": context.store_id,
                "startTime": TypeParser.format_timestamp(context.window_start, self.tz),
                "endTime": TypeParser.format_timestamp(context.window_end, self.tz),
            }
        )
        records, _ = self.api.get_paginated(
            endpoint="/Order/GetRefundPayLogList",
            params=params,
            page_size=self.config.get("api.page_size", 200),
            data_path=("data",),
        )
        return {"records": records}

    def transform(self, extracted: dict, context: TaskContext) -> dict:
        parsed, skipped = [], 0
        for raw in extracted.get("records", []):
            mapped = self._parse_refund(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 = RefundLoader(self.db)
        inserted, updated, loader_skipped = loader.upsert_refunds(transformed["records"])
        return {
            "fetched": transformed["fetched"],
            "inserted": inserted,
            "updated": updated,
            "skipped": transformed["skipped"] + loader_skipped,
            "errors": 0,
        }

    def _parse_refund(self, raw: dict, store_id: int) -> dict | None:
        refund_id = TypeParser.parse_int(raw.get("id"))
        if not refund_id:
            self.logger.warning("跳过缺少退款ID的数据: %s", raw)
            return None

        return {
            "store_id": store_id,
            "refund_id": refund_id,
            "site_id": TypeParser.parse_int(raw.get("site_id") or raw.get("siteId")),
            "tenant_id": TypeParser.parse_int(raw.get("tenant_id") or raw.get("tenantId")),
            "pay_amount": TypeParser.parse_decimal(raw.get("pay_amount")),
            "pay_status": raw.get("pay_status"),
            "pay_time": TypeParser.parse_timestamp(
                raw.get("pay_time") or raw.get("payTime"), self.tz
            ),
            "create_time": TypeParser.parse_timestamp(
                raw.get("create_time") or raw.get("createTime"), self.tz
            ),
            "relate_type": raw.get("relate_type"),
            "relate_id": TypeParser.parse_int(raw.get("relate_id")),
            "payment_method": raw.get("payment_method"),
            "refund_amount": TypeParser.parse_decimal(raw.get("refund_amount")),
            "action_type": raw.get("action_type"),
            "pay_terminal": raw.get("pay_terminal"),
            "operator_id": TypeParser.parse_int(raw.get("operator_id")),
            "channel_pay_no": raw.get("channel_pay_no"),
            "channel_fee": TypeParser.parse_decimal(raw.get("channel_fee")),
            "is_delete": raw.get("is_delete"),
            "member_id": TypeParser.parse_int(raw.get("member_id")),
            "member_card_id": TypeParser.parse_int(raw.get("member_card_id")),
            "raw_data": json.dumps(raw, ensure_ascii=False),
        }
