import { Router } from 'express'

import {
  listAdminWorkerFinanceRequests,
  reviewAdminWorkerFinanceRequest,
} from '../../../services/worker-platform/index.js'
import { createJsonHandler, requireAdminRoles } from '../session.js'

const router = Router()

router.get(
  '/worker-platform/finance-requests',
  requireAdminRoles(['admin', 'operator', 'support', 'finance']),
  createJsonHandler((req) => listAdminWorkerFinanceRequests(req.query), {
    successMessage: 'ok',
    errorMessage: '读取资金申请失败',
    scope: '[admin/worker-platform/finance-requests]',
  }),
)

router.post(
  '/worker-platform/finance-requests/:requestId/review',
  requireAdminRoles(['admin', 'operator', 'finance']),
  createJsonHandler(
    (req) =>
      reviewAdminWorkerFinanceRequest(
        String(req.params.requestId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '资金申请已处理',
      errorMessage: '处理资金申请失败',
      scope: '[admin/worker-platform/finance-requests/:requestId/review]',
      audit: (req, data) => ({
        action: 'worker_finance_request_reviewed',
        targetType: 'worker_finance_request',
        targetId: String(req.params.requestId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

export default router
