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

module.exports = {
  "POST /biz_user/page": async (ctx) => {
    const body = getRequestBody(ctx);
    const data = await crud.page("biz_user", body);
    ctx.success({ rows: data.rows, count: data.count });
  },
  "POST /biz_user/add": async (ctx) => {
    const body = getRequestBody(ctx);
    const row = await crud.add("biz_user", body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: row.id,
      action: "biz_user.add",
      resource_type: "biz_user",
      resource_id: row.id,
      detail: { name: row.name },
    });
    ctx.success(row);
  },
  "POST /biz_user/edit": async (ctx) => {
    const body = getRequestBody(ctx);
    await crud.edit("biz_user", body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: body.id,
      action: "biz_user.edit",
      resource_type: "biz_user",
      resource_id: body.id,
    });
    ctx.success({});
  },
  "POST /biz_user/del": async (ctx) => {
    const body = getRequestBody(ctx);
    await crud.del("biz_user", body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: body.id,
      action: "biz_user.del",
      resource_type: "biz_user",
      resource_id: body.id,
    });
    ctx.success({});
  },
  "GET /biz_user/detail": async (ctx) => {
    const q = ctx.query || {};
    const id = q.id || q.ID;
    const user = await crud.detail("biz_user", { id });
    if (!user) {
      return ctx.fail("用户不存在");
    }
    const subscriptions = await baseModel.biz_subscription.findAll({
      where: { user_id: id },
      order: [["id", "DESC"]],
      limit: 10,
    });
    const tokenCount = await baseModel.biz_api_token.count({
      where: { user_id: id },
    });
    ctx.success({
      user,
      subscriptions,
      tokenCount,
    });
  },
  "GET /biz_user/all": async (ctx) => {
    const rows = await crud.all("biz_user");
    ctx.success(rows);
  },
  "POST /biz_user/disable": async (ctx) => {
    const body = getRequestBody(ctx);
    const id = body.id;
    if (id == null) return ctx.fail("缺少 id");
    await baseModel.biz_user.update({ status: "disabled" }, { where: { id } });
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: id,
      action: "biz_user.disable",
      resource_type: "biz_user",
      resource_id: id,
    });
    ctx.success({});
  },
  "POST /biz_user/export": async (ctx) => {
    const body = getRequestBody(ctx);
    const res = await crud.exportCsv("biz_user", body);
    ctx.success(res);
  },
  "POST /biz_user/revoke_all_tokens": async (ctx) => {
    const body = getRequestBody(ctx);
    const userId = body.user_id != null ? body.user_id : body.id;
    if (userId == null) return ctx.fail("缺少 user_id");
    const n = await tokenLogic.revokeAllForUser(userId);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: userId,
      action: "biz_token.revoke_all",
      resource_type: "biz_user",
      resource_id: userId,
      detail: { affected: n },
    });
    ctx.success({ revoked: n });
  },
};
