import { Router } from 'express'

import { uploadSingleFile } from '../file-upload.js'
import { uploadFileAsset } from '../../services/file-storage/file-storage-service.js'
import {
  changeWorkerPassword,
  renameWorkerAccount,
  createWorkerRechargeRequest,
  createWorkerWithdrawRequest,
  createWorkerWithdrawalAccount,
  getWorkerProfile,
  listWorkerProfileFinanceRequests,
  listWorkerProfileWalletLedgers,
  saveWorkerAlipayWithdrawalQrCode,
} from '../../services/worker-platform/index.js'
import { getRequiredWorkerSession, requireActiveWorker } from './session.js'
import { createRouteHandler } from '../../utils/http.js'

const router = Router()

router.get(
  '/profile',
  createRouteHandler((req) => getWorkerProfile(getRequiredWorkerSession(req)), {
    successMessage: 'ok',
    errorMessage: '读取打手信息失败',
    scope: '[worker/profile]',
  }),
)

router.post(
  '/profile/rename',
  requireActiveWorker,
  createRouteHandler((req) => renameWorkerAccount(req.body || {}, getRequiredWorkerSession(req)), {
    successMessage: '昵称已修改',
    errorMessage: '修改昵称失败',
    scope: '[worker/profile/rename]',
  }),
)

router.get(
  '/profile/wallet-ledgers',
  createRouteHandler(
    (req) => listWorkerProfileWalletLedgers(req.query, getRequiredWorkerSession(req)),
    {
      successMessage: 'ok',
      errorMessage: '读取钱包流水失败',
      scope: '[worker/profile/wallet-ledgers]',
    },
  ),
)

router.get(
  '/profile/finance-requests',
  createRouteHandler(
    (req) => listWorkerProfileFinanceRequests(req.query, getRequiredWorkerSession(req)),
    {
      successMessage: 'ok',
      errorMessage: '读取资金申请记录失败',
      scope: '[worker/profile/finance-requests]',
    },
  ),
)

router.post(
  '/profile/recharge-requests',
  requireActiveWorker,
  createRouteHandler(
    (req) => createWorkerRechargeRequest(req.body || {}, getRequiredWorkerSession(req)),
    {
      successMessage: '充值申请已提交',
      errorMessage: '提交充值申请失败',
      scope: '[worker/profile/recharge-requests]',
    },
  ),
)

router.post(
  '/profile/withdrawal-account',
  requireActiveWorker,
  createRouteHandler(
    (req) => createWorkerWithdrawalAccount(req.body || {}, getRequiredWorkerSession(req)),
    {
      successMessage: '提现信息已添加',
      errorMessage: '添加提现信息失败',
      scope: '[worker/profile/withdrawal-account]',
    },
  ),
)

router.put(
  '/profile/withdrawal-account/alipay-qr-code',
  requireActiveWorker,
  createRouteHandler(
    (req) => saveWorkerAlipayWithdrawalQrCode(req.body || {}, getRequiredWorkerSession(req)),
    {
      successMessage: '支付宝收款二维码已保存',
      errorMessage: '保存支付宝收款二维码失败',
      scope: '[worker/profile/withdrawal-account/alipay-qr-code]',
    },
  ),
)

router.post(
  '/profile/withdraw-requests',
  requireActiveWorker,
  createRouteHandler(
    (req) => createWorkerWithdrawRequest(req.body || {}, getRequiredWorkerSession(req)),
    {
      successMessage: '提现申请已提交',
      errorMessage: '提交提现申请失败',
      scope: '[worker/profile/withdraw-requests]',
    },
  ),
)

router.post(
  '/profile/change-password',
  createRouteHandler((req) => changeWorkerPassword(req.body || {}, getRequiredWorkerSession(req)), {
    successMessage: '密码已修改',
    errorMessage: '修改密码失败',
    scope: '[worker/profile/change-password]',
  }),
)

router.post(
  '/files/upload',
  requireActiveWorker,
  uploadSingleFile,
  createRouteHandler(
    (req) => {
      const session = getRequiredWorkerSession(req)
      return uploadFileAsset({
        file: req.file,
        scene: req.body?.scene || 'worker-acceptance',
        uploaderType: 'worker',
        uploaderId: String(session.workerId),
      })
    },
    {
      successMessage: '文件已上传',
      errorMessage: '文件上传失败',
      scope: '[worker/files/upload]',
    },
  ),
)

export default router
