const crud = require("../service/biz_admin_crud");
const { getRequestBody } = crud;
const tokenLogic = require("../service/biz_token_logic");
const audit = require("../service/biz_audit_service");

module.exports = {
  "POST /biz_token/page": async (ctx) => {
    const body = getRequestBody(ctx);
    const data = await crud.page("biz_api_token", body);
    ctx.success({ rows: data.rows, count: data.count });
  },
  "POST /biz_token/create": async (ctx) => {
    const body = getRequestBody(ctx);
    const result = await tokenLogic.createToken(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: result.row.user_id,
      action: "biz_token.create",
      resource_type: "biz_api_token",
      resource_id: result.row.id,
      detail: { token_name: result.row.token_name },
    });
    ctx.success({
      id: result.row.id,
      user_id: result.row.user_id,
      plan_id: result.row.plan_id,
      token_name: result.row.token_name,
      expire_at: result.row.expire_at,
      plain_token: result.plain_token,
      warn: result.warn,
    });
  },
  "POST /biz_token/revoke": async (ctx) => {
    const body = getRequestBody(ct