// Storage keys
const ACCESS_TOKEN_KEY = 'admin_access_token'
const REFRESH_TOKEN_KEY = 'admin_refresh_token'
const TOKEN_EXPIRES_KEY = 'admin_token_expires'

// Legacy key for backward compatibility
const LEGACY_TOKEN_KEY = 'admin_token'

// Token expiration threshold (5 minutes in milliseconds)
const EXPIRATION_THRESHOLD_MS = 5 * 60 * 1000

// ============ Access Token ============

/**
 * Get access token from storage
 */
export function getAccessToken(): string | null {
  return localStorage.getItem(ACCESS_TOKEN_KEY)
}

/**
 * Set access token in storage
 */
export function setAccessToken(token: string): void {
  localStorage.setItem(ACCESS_TOKEN_KEY, token)
}

/**
 * Remove access token from storage
 */
export function removeAccessToken(): void {
  localStorage.removeItem(ACCESS_TOKEN_KEY)
}

// ============ Refresh Token ============

/**
 * Get refresh token from storage
 */
export function getRefreshToken(): string | null {
  return localStorage.getItem(REFRESH_TOKEN_KEY)
}

/**
 * Set refresh token in storage
 */
export function setRefreshToken(token: string): void {
  localStorage.setItem(REFRESH_TOKEN_KEY, token)
}

/**
 * Remove refresh token from storage
 */
export function removeRefreshToken(): void {
  localStorage.removeItem(REFRESH_TOKEN_KEY)
}

// ============ Token Expiration ============

/**
 * Get token expiration timestamp from storage
 * @returns Expiration timestamp in milliseconds, or null if not set
 */
export function getTokenExpires(): number | null {
  const expires = localStorage.getItem(TOKEN_EXPIRES_KEY)
  return expires ? parseInt(expires, 10) : null
}

/**
 * Set token expiration time
 * @param expiresIn Token lifetime in seconds
 */
export function setTokenExpires(expiresIn: number): void {
  const expiresAt = Date.now() + expiresIn * 1000
  localStorage.setItem(TOKEN_EXPIRES_KEY, expiresAt.toString())
}

/**
 * Remove token expiration from storage
 */
export function removeTokenExpires(): void {
  localStorage.removeItem(TOKEN_EXPIRES_KEY)
}

/**
 * Check if the access token is about to expire (within 5 minutes)
 * @returns true if token will expire within 5 minutes, false otherwise
 */
export function isTokenExpiringSoon(): boolean {
  const expiresAt = getTokenExpires()
  if (expiresAt === null) {
    return false
  }
  const remainingTime = expiresAt - Date.now()
  return remainingTime > 0 && remainingTime <= EXPIRATION_THRESHOLD_MS
}

/**
 * Check if the access token has expired
 * @returns true if token has expired, false otherwise
 */
export function isTokenExpired(): boolean {
  const expiresAt = getTokenExpires()
  if (expiresAt === null) {
    return false
  }
  return Date.now() >= expiresAt
}

// ============ Combined Operations ============

/**
 * Clear all tokens from storage
 */
export function clearTokens(): void {
  removeAccessToken()
  removeRefreshToken()
  removeTokenExpires()
  // Also remove legacy token for backward compatibility
  localStorage.removeItem(LEGACY_TOKEN_KEY)
}

/**
 * Store both tokens and expiration time
 * @param accessToken The access token
 * @param refreshToken The refresh token
 * @param expiresIn Access token lifetime in seconds
 */
export function setTokens(accessToken: string, refreshToken: string, expiresIn: number): void {
  setAccessToken(accessToken)
  setRefreshToken(refreshToken)
  setTokenExpires(expiresIn)
}

// ============ Legacy Compatibility ============

/**
 * Get token (legacy compatibility - returns access token)
 * @deprecated Use getAccessToken() instead
 */
export function getToken(): string | null {
  // First try new key, then fall back to legacy key
  return getAccessToken() || localStorage.getItem(LEGACY_TOKEN_KEY)
}

/**
 * Set token (legacy compatibility - sets access token)
 * @deprecated Use setAccessToken() instead
 */
export function setToken(token: string): void {
  setAccessToken(token)
}

/**
 * Remove token (legacy compatibility - clears all tokens)
 * @deprecated Use clearTokens() instead
 */
export function removeToken(): void {
  clearTokens()
}
