const SESSION_COOKIE_NAME = process.env.SESSION_COOKIE_NAME || "sid";
const SESSION_TTL_DAYS = Number(process.env.SESSION_TTL_DAYS || 30);

function sessionMaxAgeMs() {
  return SESSION_TTL_DAYS * 24 * 60 * 60 * 1000;
}

function cookieName() {
  return SESSION_COOKIE_NAME;
}

function setSessionCookie(res, sessionId) {
  res.cookie(cookieName(), sessionId, {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "lax",
    path: "/",
    maxAge: sessionMaxAgeMs(),
  });
}

function clearSessionCookie(res) {
  res.clearCookie(cookieName(), {
    httpOnly: true,
    secure: process.env.NODE_ENV === "production",
    sameSite: "lax",
    path: "/",
  });
}

module.exports = {
  SESSION_TTL_DAYS,
  clearSessionCookie,
  cookieName,
  setSessionCookie,
};
