# -*- coding: utf-8 -*-
"""台费折扣任务"""

import json

from .base_task import BaseTask, TaskContext
from loaders.facts.table_discount import TableDiscountLoader
from models.parsers import TypeParser


class TableDiscountTask(BaseTask):
    """同步台费折扣/调价记录"""

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

    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="/Site/GetTaiFeeAdjustList",
            params=params,
            page_size=self.config.get("api.page_size", 200),
            data_path=("data",),
            list_key="taiFeeAdjustInfos",
        )
        return {"records": records}

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

    def _parse_discount(self, raw: dict, store_id: int) -> dict | None:
        discount_id = TypeParser.parse_int(raw.get("id"))
        if not discount_id:
            self.logger.warning("跳过缺少折扣ID的记录: %s", raw)
            return None

        table_profile = raw.get("tableProfile") or {}
        return {
            "store_id": store_id,
            "discount_id": discount_id,
            "adjust_type": raw.get("adjust_type") or raw.get("adjustType"),
            "applicant_id": TypeParser.parse_int(raw.get("applicant_id")),
            "applicant_name": raw.get("applicant_name"),
            "operator_id": TypeParser.parse_int(raw.get("operator_id")),
            "operator_name": raw.get("operator_name"),
            "ledger_amount": TypeParser.parse_decimal(raw.get("ledger_amount")),
            "ledger_count": TypeParser.parse_int(raw.get("ledger_count")),
            "ledger_name": raw.get("ledger_name"),
            "ledger_status": raw.get("ledger_status"),
            "order_settle_id": TypeParser.parse_int(raw.get("order_settle_id")),
            "order_trade_no": TypeParser.parse_int(raw.get("order_trade_no")),
            "site_table_id": TypeParser.parse_int(
                raw.get("site_table_id") or table_profile.get("id")
            ),
            "table_area_id": TypeParser.parse_int(
                raw.get("tableAreaId") or table_profile.get("site_table_area_id")
            ),
            "table_area_name": table_profile.get("site_table_area_name"),
            "create_time": TypeParser.parse_timestamp(
                raw.get("create_time") or raw.get("createTime"), self.tz
            ),
            "is_delete": raw.get("is_delete"),
            "raw_data": json.dumps(raw, ensure_ascii=False),
        }
