import { Router } from 'express'

import {
  closeClaimSession,
  confirmClaimRole,
  createClaimSession,
  getClaimDetail,
  getClaimScreenshotPath,
  getClaimSessionSummary,
  reloadClaimSession,
  redeemClaimTask,
} from '../services/claim/claim-session-service.js'
import {
  confirmKuaishouCloudClaimRole,
  getKuaishouCloudClaimGuideAssetPath,
  redeemKuaishouCloudClaim,
  verifyKuaishouCloudClaimTicket,
} from '../services/claim/kuaishou-cloud-claim-service.js'
import { buildNotFoundPayload, buildSuccessPayload, sendRouteError } from '../utils/http.js'

const router = Router()

router.get('/:token', async (req, res) => {
  try {
    const data = await getClaimDetail(req.params.token)
    res.json(buildSuccessPayload(data, 'ok'))
  } catch (error) {
    sendRouteError(res, error, '查询领取详情失败', '[claims/:token]')
  }
})

router.post('/:token/session', async (req, res) => {
  try {
    const data = await createClaimSession(req.params.token, req.body)
    res.json(buildSuccessPayload(data, '浏览器会话已创建'))
  } catch (error) {
    sendRouteError(res, error, '创建领取会话失败', '[claims/:token/session]')
  }
})

router.get('/:token/session/summary', async (req, res) => {
  try {
    const data = await getClaimSessionSummary(req.params.token)
    res.json(buildSuccessPayload(data, 'ok'))
  } catch (error) {
    sendRouteError(res, error, '查询领取会话摘要失败', '[claims/:token/session/summary]')
  }
})

router.post('/:token/session/refresh', async (req, res) => {
  try {
    const data = await reloadClaimSession(req.params.token)
    res.json(buildSuccessPayload(data, data.session ? (data.session.notice || '后端页面已刷新') : '领取会话已重置'))
  } catch (error) {
    sendRouteError(res, error, '刷新领取会话失败', '[claims/:token/session/refresh]')
  }
})

router.delete('/:token/session', async (req, res) => {
  try {
    const data = await closeClaimSession(req.params.token)
    res.json(buildSuccessPayload(data, '领取会话已关闭'))
  } catch (error) {
    sendRouteError(res, error, '关闭领取会话失败', '[claims/:token/session]')
  }
})

router.post('/:token/confirm-role', async (req, res) => {
  try {
    const data = await confirmClaimRole(req.params.token)
    res.json(buildSuccessPayload(data, '角色已确认'))
  } catch (error) {
    sendRouteError(res, error, '确认角色失败', '[claims/:token/confirm-role]')
  }
})

router.post('/:token/redeem', async (req, res) => {
  try {
    const data = await redeemClaimTask(req.params.token)
    res.json(buildSuccessPayload(data, '兑换完成'))
  } catch (error) {
    sendRouteError(res, error, '执行领取兑换失败', '[claims/:token/redeem]')
  }
})

router.post('/:token/kuaishou-cloud/verify-ticket', async (req, res) => {
  try {
    const data = await verifyKuaishouCloudClaimTicket(req.params.token, req.body)
    res.json(buildSuccessPayload(data, '核销码校验成功'))
  } catch (error) {
    sendRouteError(res, error, '校验核销码失败', '[claims/:token/kuaishou-cloud/verify-ticket]')
  }
})

router.post('/:token/kuaishou-cloud/confirm-role', async (req, res) => {
  try {
    const data = await confirmKuaishouCloudClaimRole(req.params.token)
    res.json(buildSuccessPayload(data, '角色已确认'))
  } catch (error) {
    sendRouteError(res, error, '确认角色失败', '[claims/:token/kuaishou-cloud/confirm-role]')
  }
})

router.post('/:token/kuaishou-cloud/redeem', async (req, res) => {
  try {
    const data = await redeemKuaishouCloudClaim(req.params.token)
    res.json(buildSuccessPayload(data, '兑换请求已提交'))
  } catch (error) {
    sendRouteError(res, error, '兑换失败', '[claims/:token/kuaishou-cloud/redeem]')
  }
})

router.get('/:token/screenshot', async (req, res) => {
  try {
    const screenshotPath = await getClaimScreenshotPath(req.params.token)
    res.sendFile(screenshotPath)
  } catch (error) {
    sendRouteError(res, error, '读取领取截图失败', '[claims/:token/screenshot]')
  }
})

router.get('/assets/kuaishou-cloud/:filename', async (req, res) => {
  try {
    const filePath = await getKuaishouCloudClaimGuideAssetPath(req.params.filename)
    res.sendFile(filePath)
  } catch (error) {
    sendRouteError(res, error, '读取指引图片失败', '[claims/assets/kuaishou-cloud/:filename]')
  }
})

router.use((req, res) => {
  res.status(404).json(buildNotFoundPayload(req))
})

export default router
