import { Router } from 'express'

import {
  creditAdminWorkerWallet,
  getAdminWorkerDetail,
  listAdminWorkerDepositUnfreezes,
  listAdminWorkerUsers,
  listAdminWorkerWalletLedgers,
  listAdminWorkerWithdrawalAccounts,
  resetAdminWorkerPassword,
  reviewAdminWorkerUser,
  saveAdminWorkerWithdrawalAccount,
  setAdminWorkerPersonalDepositFreeAmount,
} from '../../../services/worker-platform/index.js'
import { createJsonHandler, requireAdminRoles } from '../session.js'

const router = Router()

router.get(
  '/worker-platform/workers',
  requireAdminRoles(['admin', 'operator', 'support']),
  createJsonHandler((req) => listAdminWorkerUsers(req.query), {
    successMessage: 'ok',
    errorMessage: '读取打手列表失败',
    scope: '[admin/worker-platform/workers]',
  }),
)

router.get(
  '/worker-platform/workers/:workerId/detail',
  requireAdminRoles(['admin', 'operator', 'support']),
  createJsonHandler((req) => getAdminWorkerDetail(String(req.params.workerId || '')), {
    successMessage: 'ok',
    errorMessage: '读取打手详情失败',
    scope: '[admin/worker-platform/workers/:workerId/detail]',
  }),
)

router.get(
  '/worker-platform/workers/:workerId/wallet-ledgers',
  requireAdminRoles(['admin', 'operator', 'support']),
  createJsonHandler(
    (req) => listAdminWorkerWalletLedgers(String(req.params.workerId || ''), req.query),
    {
      successMessage: 'ok',
      errorMessage: '读取打手资金流水失败',
      scope: '[admin/worker-platform/workers/:workerId/wallet-ledgers]',
    },
  ),
)

router.get(
  '/worker-platform/workers/:workerId/deposit-unfreezes',
  requireAdminRoles(['admin', 'operator', 'support']),
  createJsonHandler(
    (req) => listAdminWorkerDepositUnfreezes(String(req.params.workerId || ''), req.query),
    {
      successMessage: 'ok',
      errorMessage: '读取打手待解冻记录失败',
      scope: '[admin/worker-platform/workers/:workerId/deposit-unfreezes]',
    },
  ),
)

router.post(
  '/worker-platform/workers/:workerId/review',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler(
    (req) => reviewAdminWorkerUser(String(req.params.workerId || ''), req.body || {}),
    {
      successMessage: '打手账号状态已更新',
      errorMessage: '更新打手账号状态失败',
      scope: '[admin/worker-platform/workers/:workerId/review]',
      audit: (req, data) => ({
        action: 'worker_reviewed',
        targetType: 'worker',
        targetId: String(req.params.workerId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/workers/:workerId/reset-password',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler(
    (req) => resetAdminWorkerPassword(String(req.params.workerId || ''), req.body || {}),
    {
      successMessage: '打手密码已重置',
      errorMessage: '重置打手密码失败',
      scope: '[admin/worker-platform/workers/:workerId/reset-password]',
      audit: (req, data) => ({
        action: 'worker_password_reset',
        targetType: 'worker',
        targetId: String(req.params.workerId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/workers/:workerId/personal-deposit-free-amount',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler(
    (req) =>
      setAdminWorkerPersonalDepositFreeAmount(String(req.params.workerId || ''), req.body || {}),
    {
      successMessage: '打手个人免押额度已更新',
      errorMessage: '更新个人免押额度失败',
      scope: '[admin/worker-platform/workers/:workerId/personal-deposit-free-amount]',
      audit: (req, data) => ({
        action: 'worker_personal_deposit_free_amount_updated',
        targetType: 'worker',
        targetId: String(req.params.workerId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/workers/:workerId/wallet-credit',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler(
    (req) => creditAdminWorkerWallet(String(req.params.workerId || ''), req.body || {}),
    {
      successMessage: '打手余额已增加',
      errorMessage: '人工充值失败',
      scope: '[admin/worker-platform/workers/:workerId/wallet-credit]',
      audit: (req, data) => ({
        action: 'worker_wallet_credited',
        targetType: 'worker',
        targetId: String(req.params.workerId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.get(
  '/worker-platform/workers/:workerId/withdrawal-accounts',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler((req) => listAdminWorkerWithdrawalAccounts(String(req.params.workerId || '')), {
    successMessage: 'ok',
    errorMessage: '读取打手提现信息失败',
    scope: '[admin/worker-platform/workers/:workerId/withdrawal-accounts]',
  }),
)

router.post(
  '/worker-platform/workers/:workerId/withdrawal-accounts/:accountChannel',
  requireAdminRoles(['admin', 'operator']),
  createJsonHandler(
    (req) =>
      saveAdminWorkerWithdrawalAccount(
        String(req.params.workerId || ''),
        String(req.params.accountChannel || ''),
        req.body || {},
      ),
    {
      successMessage: '打手提现信息已保存',
      errorMessage: '保存打手提现信息失败',
      scope: '[admin/worker-platform/workers/:workerId/withdrawal-accounts/:accountChannel]',
      audit: (req) => ({
        action: 'worker_withdrawal_account_saved',
        targetType: 'worker',
        targetId: String(req.params.workerId || ''),
        data: { accountChannel: String(req.params.accountChannel || '') },
      }),
    },
  ),
)

export default router
