const baseModel = require("../../middleware/baseModel");
const { build_search_where, normalize_for_write } = require("../utils/query_helpers");
const audit = require("../utils/biz_audit");

module.exports = {
  "POST /biz_plan/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_plan = baseModel.biz_plan;
    const where = build_search_where(biz_plan, seach_option);
    const { count, rows } = await biz_plan.findAndCountAll({
      where,
      offset,
      limit: page_size,
      order: [["id", "DESC"]],
    });
    ctx.success({ rows, count });
  },
  "POST /biz_plan/add": async (ctx) => {
    const body = ctx.getBody();
    const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: true });
    const row = await baseModel.biz_plan.create(payload);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_plan.add",
      resource_type: "biz_plan",
      resource_id: row.id,
      detail: { plan_code: row.plan_code },
    });
    ctx.success(row);
  },
  "POST /biz_plan/edit": async (ctx) => {
    const body = ctx.getBody();
    const id = body.id;
    if (id === undefined || id === null || id === "") throw new Error("缺少 id");
    const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: false });
    delete payload.id;
    await baseModel.biz_plan.update(payload, { where: { id } });
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_plan.edit",
      resource_type: "biz_plan",
      resource_id: body.id,
    });
    ctx.success({});
  },
  "POST /biz_plan/del": async (ctx) => {
    const body = ctx.getBody();
    const id = body.id !== undefined ? body.id : body;
    if (id === undefined || id === null || id === "") throw new Error("缺少 id");
    await baseModel.biz_plan.destroy({ where: { id } });
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_plan.del",
      resource_type: "biz_plan",
      resource_id: id,
    });
    ctx.success({});
  },
  "GET /biz_plan/detail": async (ctx) => {
    const q = ctx.query || {};
    const id = q.id || q.ID;
    if (id === undefined || id === null || id === "") throw new Error("缺少 id");
    const row = await baseModel.biz_plan.findByPk(id);
    ctx.success(row);
  },
  "GET /biz_plan/all": async (ctx) => {
    const rows = await baseModel.biz_plan.findAll({
      limit: 2000,
      order: [["id", "DESC"]],
    });
    ctx.success(rows);
  },
  "POST /biz_plan/toggle": async (ctx) => {
    const body = ctx.getBody();
    const id = body.id;
    if (id == null) return ctx.fail("缺少 id");
    const row = await baseModel.biz_plan.findByPk(id);
    if (!row) return ctx.fail("套餐不存在");
    const next = row.status === "active" ? "inactive" : "active";
    await row.update({ status: next });
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_plan.toggle",
      resource_type: "biz_plan",
      resource_id: id,
      detail: { status: next },
    });
    ctx.success({ status: next });
  },
  "POST /biz_plan/export": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const biz_plan = baseModel.biz_plan;
    const where = build_search_where(biz_plan, param.seachOption || {});
    const rows = await biz_plan.findAll({
      where,
      limit: 10000,
      order: [["id", "DESC"]],
    });
    ctx.success({ rows });
  },
};
