import { Router } from 'express'

import {
  createAdminKuaishouFeifeiTestOrder,
  getAdminKuaishouFeifeiConfig,
  listAdminKuaishouFeifeiProducts,
  matchAdminKuaishouFeifeiProduct,
  queryAdminKuaishouFeifeiTestOrder,
  syncAdminKuaishouFeifeiProductRules,
  updateAdminKuaishouFeifeiConfig,
} from '../../../services/admin/platform-config/kuaishou-feifei-service.js'
import { createJsonHandler } from '../session.js'
import type { JsonRecord } from '../../../types/json.js'

const router = Router()

router.get(
  '/kuaishou-feifei',
  createJsonHandler(() => getAdminKuaishouFeifeiConfig(), {
    successMessage: 'ok',
    errorMessage: '读取 kuaishou-feifei 配置失败',
    scope: '[admin/platform-config/kuaishou-feifei]',
  }),
)

router.post(
  '/kuaishou-feifei',
  createJsonHandler((req) => updateAdminKuaishouFeifeiConfig(req.body as JsonRecord), {
    successMessage: 'kuaishou-feifei 配置已保存',
    errorMessage: '保存 kuaishou-feifei 配置失败',
    scope: '[admin/platform-config/kuaishou-feifei]',
    audit: (_req, data) => {
      const result = data as JsonRecord
      const source = result.source as JsonRecord | undefined
      return {
        action: 'platform_kuaishou_feifei_config_updated',
        targetType: 'platform_config',
        targetId: 'kuaishou_feifei',
        data: {
          filePath: String(result.filePath || '').trim(),
          enabled: source?.enabled !== false,
          ruleCount: Array.isArray(source?.productRules) ? source.productRules.length : 0,
        },
      }
    },
  }),
)

router.post(
  '/kuaishou-feifei/match',
  createJsonHandler((req) => matchAdminKuaishouFeifeiProduct(req.body as JsonRecord), {
    successMessage: 'ok',
    errorMessage: '匹配 kuaishou-feifei 商品失败',
    scope: '[admin/platform-config/kuaishou-feifei/match]',
  }),
)

router.post(
  '/kuaishou-feifei/products',
  createJsonHandler((req) => listAdminKuaishouFeifeiProducts(req.body as JsonRecord), {
    successMessage: 'ok',
    errorMessage: '查询 kuaishou-feifei 商品失败',
    scope: '[admin/platform-config/kuaishou-feifei/products]',
  }),
)

router.post(
  '/kuaishou-feifei/sync-products',
  createJsonHandler((req) => syncAdminKuaishouFeifeiProductRules(req.body as JsonRecord), {
    successMessage: 'kuaishou-feifei 商品映射已同步',
    errorMessage: '同步 kuaishou-feifei 商品映射失败',
    scope: '[admin/platform-config/kuaishou-feifei/sync-products]',
    audit: (_req, data) => {
      const result = data as JsonRecord
      const sync = result.sync as JsonRecord | undefined

      return {
        action: 'platform_kuaishou_feifei_products_synced',
        targetType: 'platform_config',
        targetId: 'kuaishou_feifei',
        data: {
          productCount: Number(sync?.productCount || 0) || 0,
          ruleCount: Number(sync?.ruleCount || 0) || 0,
          status: String(sync?.status || '').trim(),
        },
      }
    },
  }),
)

router.post(
  '/kuaishou-feifei/test-order',
  createJsonHandler((req) => createAdminKuaishouFeifeiTestOrder(req.body as JsonRecord), {
    successMessage: 'kuaishou-feifei 测试单已创建',
    errorMessage: '创建 kuaishou-feifei 测试单失败',
    scope: '[admin/platform-config/kuaishou-feifei/test-order]',
    audit: (_req, data) => {
      const result = data as JsonRecord

      return {
        action: 'platform_kuaishou_feifei_test_order_created',
        targetType: 'platform_config',
        targetId: 'kuaishou_feifei',
        data: {
          orderNo: String(result.orderNo || '').trim(),
          platformOrderNo: String(result.platformOrderNo || '').trim(),
          productCode: String(result.productCode || '').trim(),
          rechargeStatus: Number(result.rechargeStatus || 0) || 0,
        },
      }
    },
  }),
)

router.post(
  '/kuaishou-feifei/query-order',
  createJsonHandler((req) => queryAdminKuaishouFeifeiTestOrder(req.body as JsonRecord), {
    successMessage: 'ok',
    errorMessage: '查询 kuaishou-feifei 订单失败',
    scope: '[admin/platform-config/kuaishou-feifei/query-order]',
  }),
)

export default router
