// Platform-specific app package mappings
// Maps common app names to their actual package IDs for iOS and Android

const APP_MAPPINGS = {
  // Browsers
  safari: {
    ios: 'com.apple.mobilesafari',
    android: null
  },
  chrome: {
    ios: 'com.google.chrome.ios',
    android: 'com.android.chrome'
  },
  firefox: {
    ios: 'org.mozilla.ios.Firefox',
    android: 'org.mozilla.firefox'
  },
  edge: {
    ios: 'com.microsoft.msedge',
    android: 'com.microsoft.emmx'
  },
  brave: {
    ios: 'com.brave.ios.browser',
    android: 'com.brave.browser'
  },
  opera: {
    ios: 'com.opera.OperaTouch',
    android: 'com.opera.browser'
  },
  
  // System Apps
  settings: {
    ios: 'com.apple.Preferences',
    android: 'com.android.settings'
  },
  camera: {
    ios: 'com.apple.camera',
    android: 'com.android.camera2'
  },
  photos: {
    ios: 'com.apple.mobileslideshow',
    android: 'com.google.android.apps.photos'
  },
  gallery: {
    ios: 'com.apple.mobileslideshow',
    android: 'com.google.android.apps.photos'
  },
  messages: {
    ios: 'com.apple.MobileSMS',
    android: 'com.google.android.apps.messaging'
  },
  phone: {
    ios: 'com.apple.mobilephone',
    android: 'com.google.android.dialer'
  },
  contacts: {
    ios: 'com.apple.MobileAddressBook',
    android: 'com.google.android.contacts'
  },
  calendar: {
    ios: 'com.apple.mobilecal',
    android: 'com.google.android.calendar'
  },
  clock: {
    ios: 'com.apple.mobiletimer',
    android: 'com.google.android.deskclock'
  },
  calculator: {
    ios: 'com.apple.calculator',
    android: 'com.google.android.calculator'
  },
  notes: {
    ios: 'com.apple.mobilenotes',
    android: 'com.google.android.keep'
  },
  mail: {
    ios: 'com.apple.mobilemail',
    android: 'com.google.android.gm'
  },
  gmail: {
    ios: 'com.google.Gmail',
    android: 'com.google.android.gm'
  },
  maps: {
    ios: 'com.apple.Maps',
    android: 'com.google.android.apps.maps'
  },
  
  // Social Media
  facebook: {
    ios: 'com.facebook.Facebook',
    android: 'com.facebook.katana'
  },
  instagram: {
    ios: 'com.burbn.instagram',
    android: 'com.instagram.android'
  },
  twitter: {
    ios: 'com.atebits.Tweetie2',
    android: 'com.twitter.android'
  },
  x: {
    ios: 'com.atebits.Tweetie2',
    android: 'com.twitter.android'
  },
  whatsapp: {
    ios: 'net.whatsapp.WhatsApp',
    android: 'com.whatsapp'
  },
  telegram: {
    ios: 'ph.telegra.Telegraph',
    android: 'org.telegram.messenger'
  },
  snapchat: {
    ios: 'com.toyopagroup.picaboo',
    android: 'com.snapchat.android'
  },
  tiktok: {
    ios: 'com.zhiliaoapp.musically',
    android: 'com.zhiliaoapp.musically'
  },
  linkedin: {
    ios: 'com.linkedin.LinkedIn',
    android: 'com.linkedin.android'
  },
  reddit: {
    ios: 'com.reddit.Reddit',
    android: 'com.reddit.frontpage'
  },
  
  // Entertainment
  youtube: {
    ios: 'com.google.ios.youtube',
    android: 'com.google.android.youtube'
  },
  netflix: {
    ios: 'com.netflix.Netflix',
    android: 'com.netflix.mediaclient'
  },
  spotify: {
    ios: 'com.spotify.client',
    android: 'com.spotify.music'
  },
  amazonmusic: {
    ios: 'com.amazon.mp3',
    android: 'com.amazon.mp3'
  },
  applemusic: {
    ios: 'com.apple.Music',
    android: 'com.apple.android.music'
  },
  hulu: {
    ios: 'com.hulu.plus',
    android: 'com.hulu.plus'
  },
  disneyplus: {
    ios: 'com.disney.disneyplus',
    android: 'com.disney.disneyplus'
  },
  primevideo: {
    ios: 'com.amazon.aiv.AIVApp',
    android: 'com.amazon.avod.thirdpartyclient'
  },
  
  // Communication
  zoom: {
    ios: 'us.zoom.videomeetings',
    android: 'us.zoom.videomeetings'
  },
  skype: {
    ios: 'com.skype.skype',
    android: 'com.skype.raider'
  },
  teams: {
    ios: 'com.microsoft.skype.teams',
    android: 'com.microsoft.teams'
  },
  slack: {
    ios: 'com.tinyspeck.chatlyio',
    android: 'com.Slack'
  },
  discord: {
    ios: 'com.hammerandchisel.discord',
    android: 'com.discord'
  },
  
  // Productivity
  gmail: {
    ios: 'com.google.Gmail',
    android: 'com.google.android.gm'
  },
  outlook: {
    ios: 'com.microsoft.Office.Outlook',
    android: 'com.microsoft.office.outlook'
  },
  googledrive: {
    ios: 'com.google.Drive',
    android: 'com.google.android.apps.docs'
  },
  dropbox: {
    ios: 'com.getdropbox.Dropbox',
    android: 'com.dropbox.android'
  },
  onedrive: {
    ios: 'com.microsoft.skydrive',
    android: 'com.microsoft.skydrive'
  },
  notion: {
    ios: 'notion.id',
    android: 'notion.id'
  },
  evernote: {
    ios: 'com.evernote.iPhone.Evernote',
    android: 'com.evernote'
  },
  
  // Shopping
  amazon: {
    ios: 'com.amazon.Amazon',
    android: 'com.amazon.mShop.android.shopping'
  },
  ebay: {
    ios: 'com.ebay.iphone',
    android: 'com.ebay.mobile'
  },
  walmart: {
    ios: 'com.walmart.groceries',
    android: 'com.walmart.android'
  },
  target: {
    ios: 'com.target.mobile',
    android: 'com.target.ui'
  },
  
  // Travel
  uber: {
    ios: 'com.ubercab.UberClient',
    android: 'com.ubercab'
  },
  lyft: {
    ios: 'com.ubercab.Lyft',
    android: 'me.lyft.android'
  },
  airbnb: {
    ios: 'com.airbnb.app',
    android: 'com.airbnb.android'
  },
  booking: {
    ios: 'com.booking.BookingApp',
    android: 'com.booking'
  },
  
  // Finance
  paypal: {
    ios: 'com.yourcompany.PPClient',
    android: 'com.paypal.android.p2pmobile'
  },
  venmo: {
    ios: 'com.venmo.touch.Venmo',
    android: 'com.venmo'
  },
  cashapp: {
    ios: 'com.squareup.cash',
    android: 'com.squareup.cash'
  }
};

/**
 * Get package ID for an app based on platform
 * @param {string} appName - Common name of the app (e.g., 'chrome', 'settings')
 * @param {string} platform - 'ios' or 'android'
 * @returns {string|null} - Package ID or null if not found
 */
function getPackageId(appName, platform) {
  const normalizedName = appName.toLowerCase().trim();
  const mapping = APP_MAPPINGS[normalizedName];
  
  if (!mapping) {
    return null;
  }
  
  return mapping[platform] || null;
}

/**
 * Get all available apps for a platform
 * @param {string} platform - 'ios' or 'android'
 * @returns {Array} - Array of app objects with name and packageId
 */
function getAvailableApps(platform) {
  const apps = [];
  
  for (const [appName, mapping] of Object.entries(APP_MAPPINGS)) {
    if (mapping[platform]) {
      apps.push({
        name: appName,
        packageId: mapping[platform]
      });
    }
  }
  
  return apps;
}

/**
 * Check if an app is available for a platform
 * @param {string} appName - Common name of the app
 * @param {string} platform - 'ios' or 'android'
 * @returns {boolean}
 */
function isAppAvailable(appName, platform) {
  return getPackageId(appName, platform) !== null;
}

/**
 * Get platform-specific launch command
 * @param {string} appName - Common name of the app
 * @param {string} platform - 'ios' or 'android'
 * @returns {string|null} - Launch command or null if not found
 */
function getLaunchCommand(appName, platform) {
  const packageId = getPackageId(appName, platform);
  
  if (!packageId) {
    return null;
  }
  
  return `launch ${packageId}`;
}

module.exports = {
  APP_MAPPINGS,
  getPackageId,
  getAvailableApps,
  isAppAvailable,
  getLaunchCommand
};
