/** Same base-path convention as every POC: the hub can serve at root or a subfolder. */
export const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH ?? "";
export const APP_URL = (process.env.APP_URL ?? "").replace(/\/$/, "");

/** Prefix an app-absolute path ("/x") with BASE_PATH. Idempotent; a no-op at root. */
export function withBase(path: string): string {
  if (BASE_PATH === "" || !path.startsWith("/")) return path;
  if (path === BASE_PATH || path.startsWith(BASE_PATH + "/")) return path;
  return BASE_PATH + path;
}

/** Exact public Google OAuth callback URL — APP_URL when set (proxy-safe), else derived. */
export function googleCallbackUrl(req: { url: string }): string {
  if (APP_URL) return `${APP_URL}/api/auth/google/callback`;
  return new URL(withBase("/api/auth/google/callback"), req.url).toString();
}
