const logic = require("../service/biz_subscription_logic");
const audit = require("../service/biz_audit_service");

function getRequestBody(ctx) {
  if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) {
    return ctx.request.body;
  }
  if (typeof ctx.getBody === "function") {
    return ctx.getBody() || {};
  }
  return {};
}

module.exports = {
  "POST /biz_payment/confirm-offline": async (ctx) => {
    const body = getRequestBody(ctx);
    const row = await logic.confirmOfflinePayment(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_payment.confirm_offline",
      resource_type: "biz_subscription",
      resource_id: body.subscription_id,
      detail: { payment_ref: body.payment_ref },
    });
    ctx.success(row);
  },
  "POST /biz_payment/confirm-link": async (ctx) => {
    const body = getRequestBody(ctx);
    const row = await logic.confirmLinkPayment(body);
    await audit.logAudit({
      admin_user_id: audit.pickAdminId(ctx),
      action: "biz_payment.confirm_link",
      resource_type: "biz_subscription",
      resource_id: body.subscription_id,
      detail: { payment_ref: body.payment_ref },
    });
    ctx.success(row);
  },
};
