import AsyncStorage from '@react-native-async-storage/async-storage';

/**
 * Storage keys for local preferences.
 * These are defined inline since they're only used by this service.
 */
const KEYS = {
  // Autofill configuration
  AUTOFILL_CONFIGURED: 'autofill_configured',

  // Timeouts
  CLIPBOARD_CLEAR_TIMEOUT: 'clipboard_clear_timeout',

  // Clipboard local-only (iOS: prevents Universal Clipboard sync)
  CLIPBOARD_LOCAL_ONLY: 'clipboard_local_only',

  // UI preferences
  SHOW_FOLDERS: 'items-show-folders',
  LAST_SEARCH_QUERY: 'items-last-search-query',
} as const;

/**
 * Service for managing user preferences that are stored locally (not in the vault).
 * Provides typed getters/setters with sensible defaults for all local storage settings.
 *
 * Note: This service handles UI preferences stored in AsyncStorage.
 * Security-sensitive settings (auth tokens, vault data) are handled by the native layer.
 */
export const LocalPreferencesService = {
  /**
   * Get whether autofill has been configured by the user.
   * @returns Whether autofill has been configured. Defaults to false.
   */
  async getAutofillConfigured(): Promise<boolean> {
    const value = await AsyncStorage.getItem(KEYS.AUTOFILL_CONFIGURED);
    return value === 'true';
  },

  /**
   * Set whether autofill has been configured.
   */
  async setAutofillConfigured(configured: boolean): Promise<void> {
    await AsyncStorage.setItem(KEYS.AUTOFILL_CONFIGURED, configured.toString());
  },

  /**
   * Get the clipboard clear timeout in seconds.
   * @returns Timeout in seconds. Defaults to 15.
   */
  async getClipboardClearTimeout(): Promise<number> {
    const value = await AsyncStorage.getItem(KEYS.CLIPBOARD_CLEAR_TIMEOUT);
    return value ? parseInt(value, 10) : 15;
  },

  /**
   * Set the clipboard clear timeout in seconds.
   */
  async setClipboardClearTimeout(timeout: number): Promise<void> {
    await AsyncStorage.setItem(KEYS.CLIPBOARD_CLEAR_TIMEOUT, timeout.toString());
  },

  /**
   * Get whether local-only clipboard copy is enabled on iOS.
   * When enabled, clipboard content is not synced via Universal Clipboard.
   * @returns Whether local-only copy is enabled. Defaults to true.
   */
  async getClipboardLocalOnly(): Promise<boolean> {
    const value = await AsyncStorage.getItem(KEYS.CLIPBOARD_LOCAL_ONLY);
    return value === null ? true : value === 'true';
  },

  /**
   * Set whether local-only clipboard copy is enabled on iOS.
   */
  async setClipboardLocalOnly(localOnly: boolean): Promise<void> {
    await AsyncStorage.setItem(KEYS.CLIPBOARD_LOCAL_ONLY, localOnly.toString());
  },

  /**
   * Get the show folders preference.
   * @returns Whether to show folders (true) or show all items flat (false). Defaults to true.
   */
  async getShowFolders(): Promise<boolean> {
    const value = await AsyncStorage.getItem(KEYS.SHOW_FOLDERS);
    // Default to true if not set
    return value === null ? true : value === 'true';
  },

  /**
   * Set the show folders preference.
   */
  async setShowFolders(showFolders: boolean): Promise<void> {
    await AsyncStorage.setItem(KEYS.SHOW_FOLDERS, showFolders.toString());
  },

  /**
   * Get the last search query from the items list.
   * @returns The last search query, or empty string if not set.
   */
  async getLastSearchQuery(): Promise<string> {
    const value = await AsyncStorage.getItem(KEYS.LAST_SEARCH_QUERY);
    return value || '';
  },

  /**
   * Set the last search query from the items list.
   */
  async setLastSearchQuery(query: string): Promise<void> {
    if (query) {
      await AsyncStorage.setItem(KEYS.LAST_SEARCH_QUERY, query);
    } else {
      await AsyncStorage.removeItem(KEYS.LAST_SEARCH_QUERY);
    }
  },

  /**
   * Clear all UI preferences. Can be called on logout.
   * Note: This only clears UI preferences, not security-related settings.
   */
  async clearUiPreferences(): Promise<void> {
    await AsyncStorage.removeItem(KEYS.SHOW_FOLDERS);
  },

  /**
   * Clear all preferences. Called on logout to reset everything.
   * Note: Security settings are handled by the native layer.
   */
  async clearAll(): Promise<void> {
    await Promise.all([
      AsyncStorage.removeItem(KEYS.AUTOFILL_CONFIGURED),
      AsyncStorage.removeItem(KEYS.CLIPBOARD_CLEAR_TIMEOUT),
      AsyncStorage.removeItem(KEYS.CLIPBOARD_LOCAL_ONLY),
      AsyncStorage.removeItem(KEYS.SHOW_FOLDERS),
    ]);
  },
};
