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

const URL_KEY = 'goon.backend_url';
const API_KEY = 'goon.api_key';

export interface Credentials {
  baseUrl: string;
  apiKey: string;
}

export async function loadCredentials(): Promise<Credentials | null> {
  const baseUrl = await SecureStore.getItemAsync(URL_KEY);
  const apiKey = await SecureStore.getItemAsync(API_KEY);
  if (!baseUrl || !apiKey) return null;
  return { baseUrl, apiKey };
}

export async function saveCredentials(c: Credentials): Promise<void> {
  await SecureStore.setItemAsync(URL_KEY, c.baseUrl);
  await SecureStore.setItemAsync(API_KEY, c.apiKey);
}

export async function clearCredentials(): Promise<void> {
  await SecureStore.deleteItemAsync(URL_KEY);
  await SecureStore.deleteItemAsync(API_KEY);
}
