/**
 * [C-01] JWT Authentication Utilities
 * Handle token storage, retrieval, and expiration
 */

const TOKEN_KEY = 'inventory_token';
const USER_KEY = 'inventory_user';

export interface AuthToken {
  access_token: string;
  token_type: string;
  user_id: number;
  username: string;
  role: string;
}

export interface AuthUser {
  id: number;
  username: string;
  role: string;
}

/**
 * Save JWT token to localStorage
 */
export const saveToken = (token: AuthToken): void => {
  localStorage.setItem(TOKEN_KEY, token.access_token);
  localStorage.setItem(USER_KEY, JSON.stringify({
    id: token.user_id,
    username: token.username,
    role: token.role
  }));
};

/**
 * Get JWT token from localStorage
 */
export const getToken = (): string | null => {
  if (typeof window === 'undefined') return null;
  return localStorage.getItem(TOKEN_KEY);
};

/**
 * Get current user info from localStorage
 */
export const getCurrentUser = (): AuthUser | null => {
  if (typeof window === 'undefined') return null;
  const userJson = localStorage.getItem(USER_KEY);
  if (!userJson) return null;
  try {
    return JSON.parse(userJson);
  } catch {
    return null;
  }
};

/**
 * Check if user is authenticated (token exists)
 */
export const isAuthenticated = (): boolean => {
  return !!getToken();
};

/**
 * Clear token and user data (logout)
 */
export const clearAuth = (): void => {
  localStorage.removeItem(TOKEN_KEY);
  localStorage.removeItem(USER_KEY);
};

/**
 * Get Bearer token for API requests
 */
export const getAuthHeader = (): { Authorization: string } | {} => {
  const token = getToken();
  if (!token) return {};
  return { Authorization: `Bearer ${token}` };
};
