import { Router } from "express";

import {
  getAdminAgisoShopConfigs,
  updateAdminAgisoShopConfigs,
} from "../../../services/admin/platform-config/service.js";
import type { AdminAgisoShopConfigRouteBody } from "../../../types/admin-route-inputs.js";
import type { AdminAgisoShopConfigSaveResponse } from "../../../types/admin-write-models.js";
import { createJsonHandler } from "../shared.js";

const router = Router();

router.get(
  "/agiso-shops",
  createJsonHandler(() => getAdminAgisoShopConfigs(), {
    successMessage: "ok",
    errorMessage: "读取 Agiso 店铺配置失败",
    scope: "[admin/platform-config/agiso-shops]",
  })
);

router.post(
  "/agiso-shops",
  createJsonHandler(
    (req) =>
      updateAdminAgisoShopConfigs(req.body as AdminAgisoShopConfigRouteBody),
    {
      successMessage: "Agiso 店铺配置已保存",
      errorMessage: "保存 Agiso 店铺配置失败",
      scope: "[admin/platform-config/agiso-shops]",
      audit: (_req, data) => {
        const result = data as AdminAgisoShopConfigSaveResponse;
        return {
          action: "platform_shop_config_updated",
          targetType: "platform_config",
          targetId: "agiso_shops",
          data: {
            shopCount: result.shops.length,
            filePath: result.filePath,
          },
        };
      },
    }
  )
);

export default router;
