import * as SecureStore from 'expo-secure-store';

const TOKEN_KEY = 'tttagebuch.token';
const USERNAME_KEY = 'tttagebuch.username';

export async function loadAuthFromStorage(): Promise<{ token: string | null; username: string | null }> {
  const [token, username] = await Promise.all([
    SecureStore.getItemAsync(TOKEN_KEY),
    SecureStore.getItemAsync(USERNAME_KEY),
  ]);
  return { token: token || null, username: username || null };
}

export async function saveAuthToStorage(token: string | null, username: string | null) {
  if (token) {
    await SecureStore.setItemAsync(TOKEN_KEY, token);
  } else {
    await SecureStore.deleteItemAsync(TOKEN_KEY);
  }

  if (username) {
    await SecureStore.setItemAsync(USERNAME_KEY, username);
  } else {
    await SecureStore.deleteItemAsync(USERNAME_KEY);
  }
}

