// import * as Keychain from 'react-native-keychain';

// const OPTIONS = {
//   accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,
//   securityLevel: Keychain.SECURITY_LEVEL.ANY, // 🔥 critical fix
//   storage: Keychain.STORAGE_TYPE.AES, // 🔥 avoids keystore issues
// };


// export const set_item = async (key, data) => {
//   if (key && data) {
//     try {
//       await Keychain.setInternetCredentials(
//         key,
//         key,
//         data,
//         OPTIONS // 👈 FIX
//       );
//     } catch (err) {
//       console.log('Cannot save:', key, err);
//     }
//   }
// };


// export const get_item = async (key) => {
//   try {
//     const creds = await Keychain.getInternetCredentials(key);
//     return creds ? creds.password : null;
//   } catch (err) {
//     console.log('Cannot get:', key, err);
//     return null;
//   }
// };

// export const clear_item = async (key) => {
//   try {
//     await Keychain.resetInternetCredentials({
//       server: key,   // 👈 yaha object dena zaroori hai
//     });
//     console.log('Credentials reset for:', key);
//   } catch (err) {
//     console.log('Cannot reset:', key, err);
//   }
// };


// export const clear_all = async () => {
//   // Retrieve the credentials
//   const allkeys = await Keychain.getAllGenericPasswordServices();
//   console.log(allkeys);
// }

import { createMMKV } from 'react-native-mmkv'

export const storage = createMMKV({
  id: `user-storage`,
  encryptionKey: 'hunter2',
  encryptionType: 'AES-256',
  mode: 'multi-process',
  readOnly: false,
  compareBeforeSet: false,
})

/**
 * Save item
 * @param {string} key
 * @param {any} data
 */
export const set_item = async (key, data) => {
  try {
    if (!key) return;

    // Convert non-string data to JSON
    const value =
      typeof data === 'string' ? data : JSON.stringify(data);

    storage.set(key, value);
    //console.log("set_data",key,value)
  } catch (err) {
    console.log('❌ Cannot save:', key, err);
  }
};

/**
 * Get item
 * @param {string} key
 * @returns {any}
 */
export const get_item = async (key) => {
  try {
    if (!key) return null;

    const value = storage.getString(key);

    if (value === undefined) return null;

    // Try parsing JSON
    try {
      return (value);
    } catch {
      return value;
    }
  } catch (err) {
    console.log('❌ Cannot get:', key, err);
    return null;
  }
};

/**
 * Delete single item
 * @param {string} key
 */
export const clear_item = async (key) => {
  try {
    if (!key) return;

    storage.remove(key);
    console.log('✅ Deleted:', key);
  } catch (err) {
    console.log('❌ Cannot delete:', key, err);
  }
};

/**
 * Clear all storage
 */
export const clear_all = async () => {
  try {
    storage.clearAll();
    console.log('✅ All storage cleared');
  } catch (err) {
    console.log('❌ Cannot clear all:', err);
  }
};

/**
 * Get all keys (extra utility)
 */
export const get_all_keys = () => {
  try {
    return storage.getAllKeys();
  } catch (err) {
    console.log('❌ Cannot get keys:', err);
    return [];
  }
};

/**
 * Check if key exists (extra utility)
 */
export const has_key = (key) => {
  try {
    return storage.contains(key);
  } catch (err) {
    console.log('❌ Cannot check key:', key, err);
    return false;
  }
};