import { Router } from 'express'

import {
  listAdminWorkerCancelRequests,
  listAdminWorkerOrderFeedbacks,
  resolveAdminWorkerOrderFeedback,
  reviewAdminWorkerCancelRequest,
} from '../../../services/worker-platform/index.js'
import { createJsonHandler, requireAdminPermission } from '../session.js'

const router = Router()

router.get(
  '/worker-platform/cancel-requests',
  requireAdminPermission('worker_cancel_request.review'),
  createJsonHandler((req) => listAdminWorkerCancelRequests(req.query), {
    successMessage: 'ok',
    errorMessage: '读取打手退单申请失败',
    scope: '[admin/worker-platform/cancel-requests]',
  }),
)

router.get(
  '/worker-platform/feedbacks',
  requireAdminPermission('worker_order.manage_problem'),
  createJsonHandler((req) => listAdminWorkerOrderFeedbacks(req.query), {
    successMessage: 'ok',
    errorMessage: '读取问题反馈失败',
    scope: '[admin/worker-platform/feedbacks]',
  }),
)

router.post(
  '/worker-platform/feedbacks/:feedbackId/resolve',
  requireAdminPermission('worker_order.manage_problem'),
  createJsonHandler(
    (req) =>
      resolveAdminWorkerOrderFeedback(
        String(req.params.feedbackId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '问题反馈已处理',
      errorMessage: '处理问题反馈失败',
      scope: '[admin/worker-platform/feedbacks/:feedbackId/resolve]',
    },
  ),
)

router.post(
  '/worker-platform/cancel-requests/:requestId/review',
  requireAdminPermission('worker_cancel_request.review'),
  createJsonHandler(
    (req) =>
      reviewAdminWorkerCancelRequest(
        String(req.params.requestId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '打手退单申请已审核',
      errorMessage: '审核打手退单申请失败',
      scope: '[admin/worker-platform/cancel-requests/:requestId/review]',
      audit: (req, data) => ({
        action: 'worker_cancel_request_reviewed',
        targetType: 'worker_cancel_request',
        targetId: String(req.params.requestId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

export default router
