const Router = require('@koa/router')
const joi = require('joi')
const commonCtrl = require('src/controllers/common/index.js')
const accCtrl = require('src/controllers/account/index.js')

const r = new Router({ prefix: '/api' })
module.exports = r

// set api handler middleware
r.use(commonCtrl.apiHandler())

/**
 * get account info
 * @swagger
 * @route GET /api/login
 * @group account - account apis
 * @param {string} back_url.query.required - back to url
 * @returns {RespDefault.model} default -
 */
r.get(
  '/login',
  commonCtrl.validate({
    query: {
      back_url: joi.string().required()
    }
  }),
  accCtrl.loginSSO()
)

/**
 * account logout
 * @swagger
 * @route POST /api/logout
 * @group account - account apis
 * @security JWT
 * @returns {RespDefault.model} default -
 */
r.post('/logout', commonCtrl.authorization(), accCtrl.logout())

/**
 * account get info
 * @swagger
 * @route GET /api/userinfo
 * @group account - account apis
 * @security JWT
 * @returns {RespDefault.model} default -
 */
r.get('/userinfo', commonCtrl.authorization(), accCtrl.getInfo())
