import { verifySession } from "@/lib/session";
import { cookies } from "next/headers";

export async function getCustomerAccountId() {
  const cookieStore = await cookies();
  const payload = verifySession(cookieStore.get("twb_session")?.value);
  if (!payload?.startsWith("customer:")) return null;
  const id = Number(payload.slice("customer:".length));
  return Number.isInteger(id) && id > 0 ? id : null;
}

export async function requireCustomer() {
  const id = await getCustomerAccountId();
  if (!id) return { id: null, response: Response.json({ ok: false, message: "Online banking authentication required." }, { status: 401 }) };
  return { id, response: null };
}
