/**
 * Authentication utilities
 */

export function getToken(): string | null {
  if (typeof window === "undefined") return null
  return localStorage.getItem("access_token")
}

export function setToken(token: string): void {
  localStorage.setItem("access_token", token)
}

export function removeToken(): void {
  localStorage.removeItem("access_token")
  localStorage.removeItem("user")
}

export function getUser(): any | null {
  if (typeof window === "undefined") return null
  const user = localStorage.getItem("user")
  return user ? JSON.parse(user) : null
}

export function isAuthenticated(): boolean {
  return getToken() !== null
}

export function logout(): void {
  removeToken()
  if (typeof window !== "undefined") {
    window.location.href = "/login"
  }
}
