import { Router } from 'express'

import {
  getWorkerAfterSalesCaseSummary,
  getWorkerAnnouncement,
  getWorkerCancelRequestSummary,
  getWorkerMyOrderEvents,
  getWorkerMyOrderOverview,
  getWorkerOrderFeedbackSummary,
  listWorkerMyOrders,
  markWorkerOrderRead,
  remindWorkerOrderAcceptance,
  saveWorkerOrderAcceptanceDraft,
  saveWorkerOrderNote,
  submitWorkerAfterSalesCaseResponse,
  submitWorkerCancelRequest,
  submitWorkerOrderAcceptance,
  submitWorkerOrderFeedback,
  submitWorkerOrderGiftMaterial,
  supplementWorkerAcceptedOrderEvidence,
} from '../../services/worker-platform/index.js'
import { getRequiredWorkerSession, requireActiveWorker } from './session.js'
import { createRouteHandler } from '../../utils/http.js'

const router = Router()

router.get(
  '/orders',
  requireActiveWorker,
  createRouteHandler((req) => listWorkerMyOrders(req.query, getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取我的订单失败',
    scope: '[worker/orders]',
  }),
)

router.get(
  '/orders/overview',
  requireActiveWorker,
  createRouteHandler((req) => getWorkerMyOrderOverview(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取我的订单概览失败',
    scope: '[worker/orders/overview]',
  }),
)

router.get(
  '/orders/cancel-requests',
  requireActiveWorker,
  createRouteHandler((req) => getWorkerCancelRequestSummary(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取取消申请失败',
    scope: '[worker/orders/cancel-requests]',
  }),
)

router.get(
  '/announcement',
  requireActiveWorker,
  createRouteHandler((req) => getWorkerAnnouncement(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取公告失败',
    scope: '[worker/announcement]',
  }),
)

router.get(
  '/orders/feedbacks',
  requireActiveWorker,
  createRouteHandler((req) => getWorkerOrderFeedbackSummary(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取问题反馈失败',
    scope: '[worker/orders/feedbacks]',
  }),
)

router.get(
  '/orders/after-sales-cases',
  requireActiveWorker,
  createRouteHandler((req) => getWorkerAfterSalesCaseSummary(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取售后问题单失败',
    scope: '[worker/orders/after-sales-cases]',
  }),
)

router.post(
  '/orders/after-sales-cases/:caseId/response',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      submitWorkerAfterSalesCaseResponse(
        String(req.params.caseId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '售后说明已提交，等待客服处理',
      errorMessage: '提交售后说明失败',
      scope: '[worker/orders/after-sales-cases/:caseId/response]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/feedbacks',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      submitWorkerOrderFeedback(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '问题反馈已提交，等待客服处理',
      errorMessage: '提交问题反馈失败',
      scope: '[worker/orders/:workOrderId/feedbacks]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/cancel-requests',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      submitWorkerCancelRequest(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '取消申请已提交，等待客服审核',
      errorMessage: '提交取消申请失败',
      scope: '[worker/orders/:workOrderId/cancel-requests]',
    },
  ),
)

router.get(
  '/orders/:workOrderId/events',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      getWorkerMyOrderEvents(String(req.params.workOrderId || ''), getRequiredWorkerSession(req)),
    {
      successMessage: 'ok',
      errorMessage: '读取订单流转记录失败',
      scope: '[worker/orders/:workOrderId/events]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/read',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      markWorkerOrderRead(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '订单已标记为已读',
      errorMessage: '标记订单已读失败',
      scope: '[worker/orders/:workOrderId/read]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/note',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      saveWorkerOrderNote(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '备注已保存',
      errorMessage: '保存备注失败',
      scope: '[worker/orders/:workOrderId/note]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/submit-acceptance',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      submitWorkerOrderAcceptance(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '验收资料已提交',
      errorMessage: '提交验收资料失败',
      scope: '[worker/orders/:workOrderId/submit-acceptance]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/gift-material',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      submitWorkerOrderGiftMaterial(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '好友赠送资料已提交，等待后台确认加好友',
      errorMessage: '提交好友赠送资料失败',
      scope: '[worker/orders/:workOrderId/gift-material]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/remind-acceptance',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      remindWorkerOrderAcceptance(
        String(req.params.workOrderId || ''),
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '已催促管理员验收',
      errorMessage: '催验收失败',
      scope: '[worker/orders/:workOrderId/remind-acceptance]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/acceptance-draft',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      saveWorkerOrderAcceptanceDraft(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '验收图片已保存',
      errorMessage: '保存验收图片失败',
      scope: '[worker/orders/:workOrderId/acceptance-draft]',
    },
  ),
)

router.post(
  '/orders/:workOrderId/acceptance-evidence',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      supplementWorkerAcceptedOrderEvidence(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '验收图片已补充',
      errorMessage: '补充验收图片失败',
      scope: '[worker/orders/:workOrderId/acceptance-evidence]',
    },
  ),
)

export default router
