import { User } from "../types";

export const USER_COOKIE = "auth_user";
const MAX_AGE = 7 * 24 * 60 * 60;

export const serializeUserCookie = (user: User): string =>
  `${USER_COOKIE}=${encodeURIComponent(JSON.stringify(user))}; path=/; max-age=${MAX_AGE}; SameSite=Lax`;

export const clearUserCookieValue = `${USER_COOKIE}=; path=/; max-age=0; SameSite=Lax`;

export const parseUserFromCookie = (raw: string | undefined): User | null => {
  if (!raw) return null;
  try {
    return JSON.parse(decodeURIComponent(raw)) as User;
  } catch {
    return null;
  }
};
