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

module.exports = {
  "POST /biz_subscription/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_subscription = baseModel.biz_subscription;
    const where = build_search_where(biz_subscription, seach_option);
    const { count, rows } = await biz_subscription.findAndCountAll({
      where,
      offset,
      limit: page_size,
      order: [["id", "DESC"]],
    });
    ctx.success({ rows, count });
  },
  "GET /biz_subscription/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_subscription.findByPk(id);
    ctx.success(row);
  },
  "GET /biz_subscription/by_user": async (ctx) => {
    const q = ctx.query || {};
    const user_id = q.user_id || q.userId;
    if (!user_id) return ctx.fail("缺少 user_id");
    const rows = await baseModel.biz_subscription.findAll({
      where: { user_id },
      order: [["id", "DESC"]],
    });
    ctx.success(rows);
  },
  "POST /biz_subscription/open": async (ctx) => {
    const body = ctx.getBody();
    const row = await logic.openSubscription(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      biz_user_id: body.user_id,
      action: "biz_subscription.open",
      resource_type: "biz_subscription",
      resource_id: row.id,
      detail: { plan_id: body.plan_id, status: row.status },
    });
    ctx.success(row);
  },
  "POST /biz_subscription/upgrade": async (ctx) => {
    const body = ctx.getBody();
    const row = await logic.upgradeSubscription(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_subscription.upgrade",
      resource_type: "biz_subscription",
      resource_id: body.subscription_id,
      detail: { new_plan_id: body.new_plan_id },
    });
    ctx.success(row);
  },
  "POST /biz_subscription/renew": async (ctx) => {
    const body = ctx.getBody();
    const row = await logic.renewSubscription(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_subscription.renew",
      resource_type: "biz_subscription",
      resource_id: body.subscription_id,
    });
    ctx.success(row);
  },
  "POST /biz_subscription/cancel": async (ctx) => {
    const body = ctx.getBody();
    const row = await logic.cancelSubscription(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_subscription.cancel",
      resource_type: "biz_subscription",
      resource_id: body.subscription_id,
    });
    ctx.success(row);
  },
  "POST /biz_subscription/export": async (ctx) => {
    const body = ctx.getBody();
    const param = body.param || body;
    const biz_subscription = baseModel.biz_subscription;
    const where = build_search_where(biz_subscription, param.seachOption || {});
    const rows = await biz_subscription.findAll({
      where,
      limit: 10000,
      order: [["id", "DESC"]],
    });
    ctx.success({ rows });
  },
};
