const baseModel = require("../../middleware/baseModel");
const { build_search_where } = require("../utils/query_helpers");
const tokenLogic = require("../service/biz_token_logic");
const audit = require("../utils/biz_audit");

module.exports = {
  "POST /biz_token/page": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const page_option = param.pageOption || {};
    const seach_option = param.seachOption || {};
    const page_num = parseInt(page_option.page, 10) || 1;
    const page_size = parseInt(page_option.pageSize, 10) || 20;
    const offset = (page_num - 1) * page_size;
    const biz_api_token = baseModel.biz_api_token;
    const where = build_search_where(biz_api_token, seach_option);
    const { count, rows } = await biz_api_token.findAndCountAll({
      where,
      offset,
      limit: page_size,
      order: [["id", "DESC"]],
      attributes: { exclude: ["secret_cipher"] },
    });
    ctx.success({ rows, count });
  },
  "POST /biz_token/create": async (ctx) => {
    const body = ctx.getBody();
    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,
      key: result.row.key,
      token_name: result.row.token_name,
      expire_at: result.row.expire_at,
      plain_token: result.plain_token,
      warn: result.warn,
    });
  },
  "POST /biz_token/edit": async (ctx) => {
    const body = ctx.getBody();
    const row = await tokenLogic.updateToken(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: row.user_id,
      action: "biz_token.edit",
      resource_type: "biz_api_token",
      resource_id: row.id,
      detail: { token_name: row.token_name, key: row.key },
    });
    const plain = row.get ? row.get({ plain: true }) : { ...row };
    delete plain.secret_cipher;
    ctx.success(plain);
  },
  "POST /biz_token/revoke": async (ctx) => {
    const body = ctx.getBody();
    const row = await tokenLogic.revokeToken(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: row.user_id,
      action: "biz_token.revoke",
      resource_type: "biz_api_token",
      resource_id: row.id,
    });
    ctx.success({ id: row.id, status: row.status });
  },
  "POST /biz_token/regenerate": async (ctx) => {
    const body = ctx.getBody();
    const result = await tokenLogic.regenerateToken(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: result.row.user_id,
      action: "biz_token.regenerate",
      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/export": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const biz_api_token = baseModel.biz_api_token;
    const where = build_search_where(biz_api_token, param.seachOption || {});
    const rows = await biz_api_token.findAll({
      where,
      limit: 10000,
      order: [["id", "DESC"]],
      attributes: { exclude: ["secret_cipher"] },
    });
    ctx.success({ rows });
  },
};
