import { Router } from 'express'

import { getAdminSessionSummary, loginAdmin } from '../../services/admin/admin-auth-service.js'
import { createJsonHandler, extractBearerToken } from './shared.js'

const router = Router()

router.post('/auth/login', createJsonHandler(
  (req) => loginAdmin(req.body?.username, req.body?.password),
  {
    successMessage: '登录成功',
    errorMessage: '后台登录失败',
    scope: '[admin/auth/login]',
  },
))

router.get('/auth/session', createJsonHandler(
  (req) => getAdminSessionSummary(extractBearerToken(req)),
  {
    successMessage: 'ok',
    errorMessage: '读取后台登录态失败',
    scope: '[admin/auth/session]',
  },
))

router.post('/auth/logout', createJsonHandler(
  () => ({ success: true }),
  {
    successMessage: '已退出登录',
    errorMessage: '后台退出失败',
    scope: '[admin/auth/logout]',
  },
))

export default router
