// utils/secure.ts
import AsyncStorage from '@react-native-async-storage/async-storage';

const LOCK_KEY = 'lock_enabled';
const LOCK_TYPE_KEY = 'lock_type'; // pin | pattern | password
const LOCK_PASSWORD_KEY = 'lock_password';
const LOCK_BIOMETRIC_KEY = 'lock_biometric_enabled';

export const isLockEnabled = async (): Promise<boolean> =>
  (await AsyncStorage.getItem(LOCK_KEY)) === 'true';

export const setLockEnabled = async (enabled: boolean) =>
  AsyncStorage.setItem(LOCK_KEY, enabled ? 'true' : 'false');

export const getLockType = async (): Promise<
  'pin' | 'pattern' | 'password' | null
> => (await AsyncStorage.getItem(LOCK_TYPE_KEY)) as any;

export const setLockType = async (type: 'pin' | 'pattern' | 'password') =>
  AsyncStorage.setItem(LOCK_TYPE_KEY, type);

export const setLockPassword = async (password: string) =>
  AsyncStorage.setItem(LOCK_PASSWORD_KEY, password);

export const getLockPassword = async (): Promise<string | null> =>
  AsyncStorage.getItem(LOCK_PASSWORD_KEY);

export const setBiometricEnabled = async (enabled: boolean) =>
  AsyncStorage.setItem(LOCK_BIOMETRIC_KEY, enabled ? 'true' : 'false');

export const isBiometricEnabled = async (): Promise<boolean> =>
  (await AsyncStorage.getItem(LOCK_BIOMETRIC_KEY)) === 'true';

export const resetLock = async () => {
  await AsyncStorage.multiRemove([
    LOCK_KEY,
    LOCK_TYPE_KEY,
    LOCK_PASSWORD_KEY,
    LOCK_BIOMETRIC_KEY,
  ]);
};
