import { createHmac } from "node:crypto";
import { AUTH_SECRET } from "@/lib/constants";
import { constantTimeEqual } from "@/lib/crypto";

/**
 * Better Auth session cookie names — `cookiePrefix: "formbricks"` + `useSecureCookies: true`
 * (modules/auth/lib/auth.ts `advanced`) yield the browser-enforced `__Secure-` prefix on HTTPS; the
 * unprefixed form covers non-secure dev. (ENG-1054 cutover — replaces the NextAuth cookie names.)
 */
export const BETTER_AUTH_SESSION_COOKIE_NAMES = [
  "__Secure-formbricks.session_token",
  "formbricks.session_token",
] as const;

type TCookieStore = {
  get: (name: string) => { value: string } | undefined;
};

const decode = (value: string): string | null => {
  try {
    return decodeURIComponent(value);
  } catch {
    return null;
  }
};

/**
 * Verify Better Auth's signed cookie value and return the unsigned session token (the value stored
 * in `Session.sessionToken`), or `null` if absent or tampered with.
 *
 * Better Auth signs the cookie via better-call's `serializeSignedCookie`: the value is
 * `` `${token}.${signature}` `` where `signature = base64(HMAC-SHA256(token, BETTER_AUTH_SECRET))`
 * (standard base64; see better-call `crypto.mjs` `makeSignature`). We recompute that HMAC with the
 * same secret and constant-time-compare — so a raw `prisma.session.findUnique` can look the token up.
 * Fails closed: a missing secret or an invalid signature returns `null`.
 */
const verifyAndExtractSessionToken = (signedValue: string | null): string | null => {
  // The same resolved secret auth.ts hands Better Auth, so this verifies the cookies BA actually
  // signs — a mismatch rejects every session and loops the user between / and /auth/login. Fails
  // closed when no secret is set.
  const secret = AUTH_SECRET;
  if (!signedValue || !secret) {
    return null;
  }

  // The token is a cuid2 (no "."), the signature is standard base64 (no "."), so the single "."
  // cleanly separates them.
  const lastDot = signedValue.lastIndexOf(".");
  if (lastDot <= 0 || lastDot === signedValue.length - 1) {
    return null;
  }

  const token = signedValue.slice(0, lastDot);
  const signature = signedValue.slice(lastDot + 1);
  const expected = createHmac("sha256", secret).update(token).digest("base64");

  if (!constantTimeEqual(expected, signature)) {
    return null;
  }

  return token;
};

/** One cookie's decoded value from a raw `Cookie` header, or null when it is absent or empty. */
export const getCookieValueFromHeader = (cookieHeader: string, cookieName: string): string | null => {
  const cookies = cookieHeader.split(";").map((cookie) => cookie.trim());

  for (const cookie of cookies) {
    if (!cookie.startsWith(`${cookieName}=`)) {
      continue;
    }

    const cookieValue = cookie.slice(cookieName.length + 1);
    return cookieValue.length > 0 ? decode(cookieValue) : null;
  }

  return null;
};

/** The verified, unsigned Better Auth session token from a cookie store, or null. */
export const getSessionTokenFromCookieStore = (cookieStore: TCookieStore): string | null => {
  // Try every known cookie name and return the first that VERIFIES — don't bail on a present-but-invalid
  // one (e.g. a stale `__Secure-` cookie from a prior secret/scheme sitting alongside a valid cookie),
  // which would otherwise wedge the session into a redirect loop.
  for (const cookieName of BETTER_AUTH_SESSION_COOKIE_NAMES) {
    const cookie = cookieStore.get(cookieName);
    if (!cookie?.value) continue;
    const token = verifyAndExtractSessionToken(decode(cookie.value));
    if (token) return token;
  }

  return null;
};

/** The verified, unsigned Better Auth session token from a `Cookie` header, or null. */
export const getSessionTokenFromCookieHeader = (cookieHeader: string | null): string | null => {
  if (!cookieHeader) {
    return null;
  }

  for (const cookieName of BETTER_AUTH_SESSION_COOKIE_NAMES) {
    const signedValue = getCookieValueFromHeader(cookieHeader, cookieName);
    if (!signedValue) continue;
    const token = verifyAndExtractSessionToken(signedValue);
    if (token) return token;
  }

  return null;
};
