import { StorageService } from './StorageService';
import { getUserIdFromToken, isTokenExpired } from '@/lib/jwt';

const AUTH_TOKEN_KEY = 'auth_token';

export const getAuthToken = async (): Promise<string | null> => {
  return await StorageService.getItem(AUTH_TOKEN_KEY);
};

export const setAuthToken = async (token: string): Promise<boolean> => {
  return await StorageService.setItem(AUTH_TOKEN_KEY, token);
};

export const removeAuthToken = async (): Promise<boolean> => {
  return await StorageService.removeItem(AUTH_TOKEN_KEY);
};

export const clearAllAuthData = async (): Promise<boolean> => {
  return await StorageService.clearAll([AUTH_TOKEN_KEY]);
};

export const getCurrentUserId = async (): Promise<number | null> => {
  const token = await getAuthToken();
  if (!token) return null;
  return getUserIdFromToken(token);
};

export const isAuthenticated = async (): Promise<boolean> => {
  const token = await getAuthToken();
  if (!token) return false;
  return !isTokenExpired(token);
};