import { Router } from 'express'

import {
  getWorkerHallLeaderboard,
  grabWorkerHallOrder,
  joinWorkerSharingOrder,
  listWorkerHallOrders,
} from '../../services/worker-platform/index.js'
import { getRequiredWorkerSession, requireActiveWorker } from './session.js'
import { createRouteHandler } from '../../utils/http.js'

const router = Router()

router.get(
  '/hall/orders',
  requireActiveWorker,
  createRouteHandler((req) => listWorkerHallOrders(req.query, getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取抢单大厅失败',
    scope: '[worker/hall/orders]',
  }),
)

router.get(
  '/hall/leaderboard',
  requireActiveWorker,
  createRouteHandler(
    (req) => getWorkerHallLeaderboard(getRequiredWorkerSession(req), req.query?.period),
    {
      successMessage: 'ok',
      errorMessage: '读取接单排行榜失败',
      scope: '[worker/hall/leaderboard]',
    },
  ),
)

router.post(
  '/hall/orders/:workOrderId/grab',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      grabWorkerHallOrder(String(req.params.workOrderId || ''), getRequiredWorkerSession(req)),
    {
      successMessage: '抢单成功',
      errorMessage: '抢单失败',
      scope: '[worker/hall/orders/:workOrderId/grab]',
    },
  ),
)

router.post(
  '/hall/orders/:workOrderId/join-sharing',
  requireActiveWorker,
  createRouteHandler(
    (req) =>
      joinWorkerSharingOrder(
        String(req.params.workOrderId || ''),
        req.body || {},
        getRequiredWorkerSession(req),
      ),
    {
      successMessage: '拼单成功',
      errorMessage: '拼单失败',
      scope: '[worker/hall/orders/:workOrderId/join-sharing]',
    },
  ),
)

export default router
