import { Router } from "express";

import {
  consumeAdminKuaishouEticket,
  getAdminKuaishouEticketSourceConfig,
  queryAdminKuaishouEticketDetail,
  queryAdminKuaishouEticketShopInfo,
  updateAdminKuaishouEticketSourceConfig,
} from "../../../services/admin/platform-config/service.js";
import type {
  AdminKuaishouEticketConsumeRouteBody,
  AdminKuaishouEticketDetailQueryRouteBody,
  AdminKuaishouEticketShopInfoRouteBody,
  AdminKuaishouEticketSourceConfigRouteBody,
} from "../../../types/admin-route-inputs.js";
import { createJsonHandler } from "../shared.js";
import type { JsonRecord } from "./shared.js";

const router = Router();

router.get(
  "/kuaishou-eticket-source",
  createJsonHandler(() => getAdminKuaishouEticketSourceConfig(), {
    successMessage: "ok",
    errorMessage: "读取快手小店核销配置失败",
    scope: "[admin/platform-config/kuaishou-eticket-source]",
  })
);

router.post(
  "/kuaishou-eticket-source",
  createJsonHandler(
    (req) =>
      updateAdminKuaishouEticketSourceConfig(
        req.body as AdminKuaishouEticketSourceConfigRouteBody
      ),
    {
      successMessage: "快手小店核销配置已保存",
      errorMessage: "保存快手小店核销配置失败",
      scope: "[admin/platform-config/kuaishou-eticket-source]",
      audit: (_req, data) => {
        const result = data as JsonRecord;
        return {
          action: "platform_kuaishou_eticket_source_updated",
          targetType: "platform_config",
          targetId: "kuaishou_eticket_source",
          data: {
            filePath: String(result.filePath || "").trim(),
            enabled: Boolean(result.source?.enabled),
            shopCount: Array.isArray(result.source?.shops)
              ? result.source.shops.length
              : 0,
          },
        };
      },
    }
  )
);

router.post(
  "/kuaishou-eticket/query-detail",
  createJsonHandler(
    (req) =>
      queryAdminKuaishouEticketDetail(
        req.body as AdminKuaishouEticketDetailQueryRouteBody
      ),
    {
      successMessage: "快手小店核销信息查询成功",
      errorMessage: "查询快手小店核销信息失败",
      scope: "[admin/platform-config/kuaishou-eticket/query-detail]",
      audit: (req, data) => {
        const body = req.body as AdminKuaishouEticketDetailQueryRouteBody;
        const result = data as JsonRecord;
        return {
          action: "platform_kuaishou_eticket_query_detail",
          targetType: "platform_config",
          targetId:
            String(result.eTicketId || body.eTicketId || "").trim() ||
            "kuaishou_eticket",
          data: {
            result: Number(result.result || 0),
            ok: Boolean(result.ok),
            alreadyConsumed: Boolean(result.alreadyConsumed),
          },
        };
      },
    }
  )
);

router.post(
  "/kuaishou-eticket/query-shop-info",
  createJsonHandler(
    (req) =>
      queryAdminKuaishouEticketShopInfo(
        req.body as AdminKuaishouEticketShopInfoRouteBody
      ),
    {
      successMessage: "快手小店店铺信息查询成功",
      errorMessage: "查询快手小店店铺信息失败",
      scope: "[admin/platform-config/kuaishou-eticket/query-shop-info]",
      audit: (req, data) => {
        const body = req.body as AdminKuaishouEticketShopInfoRouteBody;
        const result = data as JsonRecord;
        return {
          action: "platform_kuaishou_eticket_query_shop_info",
          targetType: "platform_config",
          targetId:
            String(result.shop?.shopId || body.shopId || "").trim() ||
            "kuaishou_eticket_shop_info",
          data: {
            result: Number(result.result || 0),
            ok: Boolean(result.ok),
            kshopName: String(result.shop?.kshopName || "").trim(),
          },
        };
      },
    }
  )
);

router.post(
  "/kuaishou-eticket/consume",
  createJsonHandler(
    (req) =>
      consumeAdminKuaishouEticket(
        req.body as AdminKuaishouEticketConsumeRouteBody
      ),
    {
      successMessage: "快手小店核销请求已执行",
      errorMessage: "执行快手小店核销失败",
      scope: "[admin/platform-config/kuaishou-eticket/consume]",
      audit: (req, data) => {
        const body = req.body as AdminKuaishouEticketConsumeRouteBody;
        const result = data as JsonRecord;
        return {
          action: "platform_kuaishou_eticket_consume",
          targetType: "platform_config",
          targetId:
            String(result.eTicketId || body.eTicketId || "").trim() ||
            "kuaishou_eticket",
          data: {
            result: Number(result.result || 0),
            consumed: Boolean(result.consumed),
            alreadyConsumed: Boolean(result.alreadyConsumed),
          },
        };
      },
    }
  )
);

export default router;
