import { Router } from 'express'

import {
  acceptAdminWorkOrder,
  acceptAdminWorkOrderShare,
  cancelAdminWorkOrderShare,
  cancelAdminWorkOrderSharing,
  deductAdminWorkOrderPendingDeposit,
  markAdminWorkOrderProblem,
  resolveAdminProblemWorkOrder,
} from '../../../services/worker-platform/index.js'
import { createJsonHandler, requireAdminPermission, requireAnyAdminPermission } from '../session.js'

const router = Router()

router.post(
  '/worker-platform/orders/:workOrderId/problem',
  requireAdminPermission('worker_order.manage_problem'),
  createJsonHandler(
    (req) =>
      markAdminWorkOrderProblem(
        String(req.params.workOrderId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '订单已标记为问题单',
      errorMessage: '标记问题单失败',
      scope: '[admin/worker-platform/orders/:workOrderId/problem]',
      audit: (req, data) => ({
        action: 'work_order_marked_problem',
        targetType: 'work_order',
        targetId: String(req.params.workOrderId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/problem/resolve',
  requireAdminPermission('worker_order.manage_problem'),
  createJsonHandler(
    (req) =>
      resolveAdminProblemWorkOrder(
        String(req.params.workOrderId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '问题单已处置',
      errorMessage: '处置问题单失败',
      scope: '[admin/worker-platform/orders/:workOrderId/problem/resolve]',
      audit: (req, data) => ({
        action: 'work_order_problem_resolved',
        targetType: 'work_order',
        targetId: String(req.params.workOrderId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/accept',
  requireAdminPermission('worker_order.accept'),
  createJsonHandler(
    (req) =>
      acceptAdminWorkOrder(String(req.params.workOrderId || ''), req.adminSession?.username || ''),
    {
      successMessage: '订单已验收通过',
      errorMessage: '验收接单订单失败',
      scope: '[admin/worker-platform/orders/:workOrderId/accept]',
      audit: (req, data) => ({
        action: 'work_order_accepted',
        targetType: 'work_order',
        targetId: String(req.params.workOrderId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/shares/:shareId/accept',
  requireAdminPermission('worker_order.accept'),
  createJsonHandler(
    (req) =>
      acceptAdminWorkOrderShare(
        String(req.params.workOrderId || ''),
        String(req.params.shareId || ''),
        req.adminSession?.username || '',
      ),
    {
      successMessage: '拼单份额已验收通过',
      errorMessage: '验收拼单份额失败',
      scope: '[admin/worker-platform/orders/:workOrderId/shares/:shareId/accept]',
      audit: (req, data) => ({
        action: 'work_order_share_accepted',
        targetType: 'work_order_share',
        targetId: String(req.params.shareId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/sharing/cancel',
  requireAnyAdminPermission(['worker_order.return_to_hall', 'worker_order.cancel']),
  createJsonHandler(
    (req) =>
      cancelAdminWorkOrderSharing(
        String(req.params.workOrderId || ''),
        req.body || {},
        req.adminSession?.username || '',
        req.adminSession?.permissions || [],
      ),
    {
      successMessage: '拼单撤单处理完成',
      errorMessage: '拼单撤单失败',
      scope: '[admin/worker-platform/orders/:workOrderId/sharing/cancel]',
      audit: (req, data) => ({
        action:
          String(req.body?.action || '').trim() === 'return_to_hall'
            ? 'work_order_sharing_returned_to_hall'
            : 'work_order_sharing_cancelled',
        targetType: 'work_order',
        targetId: String(req.params.workOrderId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/shares/:shareId/cancel',
  requireAdminPermission('worker_order.cancel_share'),
  createJsonHandler(
    (req) =>
      cancelAdminWorkOrderShare(
        String(req.params.workOrderId || ''),
        String(req.params.shareId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '拼单参与者已撤单，份数已退回大厅',
      errorMessage: '撤销拼单参与者失败',
      scope: '[admin/worker-platform/orders/:workOrderId/shares/:shareId/cancel]',
      audit: (req, data) => ({
        action: 'work_order_share_cancelled',
        targetType: 'work_order_share',
        targetId: String(req.params.shareId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

router.post(
  '/worker-platform/orders/:workOrderId/deduct-pending-deposit',
  requireAdminPermission('worker_order.deduct_deposit'),
  createJsonHandler(
    (req) =>
      deductAdminWorkOrderPendingDeposit(
        String(req.params.workOrderId || ''),
        req.body || {},
        req.adminSession?.username || '',
      ),
    {
      successMessage: '待解冻押金已扣减',
      errorMessage: '扣减待解冻押金失败',
      scope: '[admin/worker-platform/orders/:workOrderId/deduct-pending-deposit]',
      audit: (req, data) => ({
        action: 'work_order_pending_deposit_deducted',
        targetType: 'work_order',
        targetId: String(req.params.workOrderId || ''),
        data: data && typeof data === 'object' ? (data as Record<string, unknown>) : {},
      }),
    },
  ),
)

export default router
