# -*- coding: utf-8 -*-
"""团购/套餐定义任务"""

import json

from .base_task import BaseTask, TaskContext
from loaders.dimensions.package import PackageDefinitionLoader
from models.parsers import TypeParser


class PackagesDefTask(BaseTask):
    """同步团购套餐定义"""

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

    def extract(self, context: TaskContext) -> dict:
        params = self._merge_common_params({"siteId": context.store_id})
        records, _ = self.api.get_paginated(
            endpoint="/PackageCoupon/QueryPackageCouponList",
            params=params,
            page_size=self.config.get("api.page_size", 200),
            data_path=("data",),
            list_key="packageCouponList",
        )
        return {"records": records}

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

    def _parse_package(self, raw: dict, store_id: int) -> dict | None:
        package_id = TypeParser.parse_int(raw.get("id"))
        if not package_id:
            self.logger.warning("跳过缺少 package id 的套餐记录: %s", raw)
            return None

        return {
            "store_id": store_id,
            "package_id": package_id,
            "package_code": raw.get("package_id") or raw.get("packageId"),
            "package_name": raw.get("package_name"),
            "table_area_id": raw.get("table_area_id"),
            "table_area_name": raw.get("table_area_name"),
            "selling_price": TypeParser.parse_decimal(
                raw.get("selling_price") or raw.get("sellingPrice")
            ),
            "duration_seconds": TypeParser.parse_int(raw.get("duration")),
            "start_time": TypeParser.parse_timestamp(
                raw.get("start_time") or raw.get("startTime"), self.tz
            ),
            "end_time": TypeParser.parse_timestamp(
                raw.get("end_time") or raw.get("endTime"), self.tz
            ),
            "type": raw.get("type"),
            "is_enabled": raw.get("is_enabled"),
            "is_delete": raw.get("is_delete"),
            "usable_count": TypeParser.parse_int(raw.get("usable_count")),
            "creator_name": raw.get("creator_name"),
            "date_type": raw.get("date_type"),
            "group_type": raw.get("group_type"),
            "coupon_money": TypeParser.parse_decimal(
                raw.get("coupon_money") or raw.get("couponMoney")
            ),
            "area_tag_type": raw.get("area_tag_type"),
            "system_group_type": raw.get("system_group_type"),
            "card_type_ids": raw.get("card_type_ids"),
            "raw_data": json.dumps(raw, ensure_ascii=False),
        }
