# -*- coding: utf-8 -*-
"""助教账号任务"""

import json

from .base_task import BaseTask, TaskContext
from loaders.dimensions.assistant import AssistantLoader
from models.parsers import TypeParser


class AssistantsTask(BaseTask):
    """同步助教账号资料"""

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

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

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

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

        return {
            "store_id": store_id,
            "assistant_id": assistant_id,
            "assistant_no": raw.get("assistant_no") or raw.get("assistantNo"),
            "nickname": raw.get("nickname"),
            "real_name": raw.get("real_name") or raw.get("realName"),
            "gender": raw.get("gender"),
            "mobile": raw.get("mobile"),
            "level": raw.get("level"),
            "team_id": TypeParser.parse_int(raw.get("team_id") or raw.get("teamId")),
            "team_name": raw.get("team_name"),
            "assistant_status": raw.get("assistant_status"),
            "work_status": raw.get("work_status"),
            "entry_time": TypeParser.parse_timestamp(
                raw.get("entry_time") or raw.get("entryTime"), self.tz
            ),
            "resign_time": TypeParser.parse_timestamp(
                raw.get("resign_time") or raw.get("resignTime"), self.tz
            ),
            "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
            ),
            "create_time": TypeParser.parse_timestamp(
                raw.get("create_time") or raw.get("createTime"), self.tz
            ),
            "update_time": TypeParser.parse_timestamp(
                raw.get("update_time") or raw.get("updateTime"), self.tz
            ),
            "system_role_id": raw.get("system_role_id"),
            "online_status": raw.get("online_status"),
            "allow_cx": raw.get("allow_cx"),
            "charge_way": raw.get("charge_way"),
            "pd_unit_price": TypeParser.parse_decimal(raw.get("pd_unit_price")),
            "cx_unit_price": TypeParser.parse_decimal(raw.get("cx_unit_price")),
            "is_guaranteed": raw.get("is_guaranteed"),
            "is_team_leader": raw.get("is_team_leader"),
            "serial_number": raw.get("serial_number"),
            "show_sort": raw.get("show_sort"),
            "is_delete": raw.get("is_delete"),
            "raw_data": json.dumps(raw, ensure_ascii=False),
        }
