/**
 * KRA - Auth Utility Functions
 * Token storage, retrieval and permission checking
 */

const TOKEN_KEY = 'token';
const USER_INFO_KEY = 'userInfo';

/**
 * Get token from localStorage
 * @returns Token string or empty string
 */
export const getToken = (): string => {
  return localStorage.getItem(TOKEN_KEY) || '';
};

/**
 * Set token to localStorage
 * @param token - Token string
 */
export const setToken = (token: string): void => {
  localStorage.setItem(TOKEN_KEY, token);
};

/**
 * Remove token from localStorage
 */
export const removeToken = (): void => {
  localStorage.removeItem(TOKEN_KEY);
};

/**
 * Check if user is logged in
 * @returns True if token exists
 */
export const isLoggedIn = (): boolean => {
  return !!getToken();
};

/**
 * Get user info from localStorage
 * @returns User info object or null
 */
export const getUserInfo = (): any => {
  const info = localStorage.getItem(USER_INFO_KEY);
  if (info) {
    try {
      return JSON.parse(info);
    } catch {
      return null;
    }
  }
  return null;
};

/**
 * Set user info to localStorage
 * @param userInfo - User info object
 */
export const setUserInfo = (userInfo: any): void => {
  localStorage.setItem(USER_INFO_KEY, JSON.stringify(userInfo));
};

/**
 * Remove user info from localStorage
 */
export const removeUserInfo = (): void => {
  localStorage.removeItem(USER_INFO_KEY);
};

/**
 * Clear all auth related storage
 */
export const clearAuth = (): void => {
  removeToken();
  removeUserInfo();
  localStorage.removeItem('originSetting');
  sessionStorage.clear();
};

/**
 * Check if user has specific authority
 * @param authorityId - Authority ID to check
 * @param userAuthorities - User's authority list
 * @returns True if user has authority
 */
export const hasAuthority = (
  authorityId: string | number,
  userAuthorities: Array<{ authorityId: string | number }>
): boolean => {
  if (!userAuthorities || !Array.isArray(userAuthorities)) {
    return false;
  }
  return userAuthorities.some((auth) => auth.authorityId === authorityId);
};

/**
 * Check if user has specific button permission
 * @param btnKey - Button permission key
 * @param btnPermissions - User's button permissions
 * @returns True if user has permission
 */
export const hasBtnPermission = (
  btnKey: string,
  btnPermissions: Record<string, boolean> | string[]
): boolean => {
  if (!btnPermissions) return false;
  if (Array.isArray(btnPermissions)) {
    return btnPermissions.includes(btnKey);
  }
  return !!btnPermissions[btnKey];
};

/**
 * Get OS type
 * @returns 'WIN' or 'MAC'
 */
export const getOsType = (): 'WIN' | 'MAC' => {
  const isWindows = /windows/i.test(navigator.userAgent);
  return isWindows ? 'WIN' : 'MAC';
};

/**
 * Set OS type to localStorage
 */
export const setOsType = (): void => {
  localStorage.setItem('osType', getOsType());
};

export default {
  getToken,
  setToken,
  removeToken,
  isLoggedIn,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
  clearAuth,
  hasAuthority,
  hasBtnPermission,
  getOsType,
  setOsType,
};
