import 'dotenv/config';
import path from 'path';
import dotenv from 'dotenv';

import type { ConfigContext, ExpoConfig } from 'expo/config';

const env = process.env;

const API_HOST = env.ENV === 'production' ? env.PRODUCTION_API_HOST : env.DEVELOPMENT_API_HOST;
const MAP_HOST = env.ENV === 'production' ? env.PRODUCTION_MAP_HOST : env.DEVELOPMENT_MAP_HOST;

const GOOGLE_MAP_PLACES_APIKEY = env.GOOGLE_MAP_PLACES_APIKEY;
const WEBSOCKET_URL =
  env.ENV === 'production' ? env.PRODUCTION_WEBSOCKET_URL : env.DEVELOPMENT_WEBSOCKET_URL;

dotenv.config({
  path: path.resolve(process.cwd(), '.env')
});

export default ({ config }: ConfigContext): ExpoConfig => ({
  ...config,
  name: 'NomadMania',
  slug: 'nomadmania-app',
  owner: 'nomadmaniaou',
  scheme: 'nm',
  // Should be updated after every production release (deploy to AppStore/PlayMarket)
  version: '2.0.24',
  // Should be updated after every dependency change
  runtimeVersion: '1.5',
  orientation: 'portrait',
  icon: './assets/icon.png',
  userInterfaceStyle: 'light',
  extra: {
    ENV: env.ENV,
    API_HOST: API_HOST,
    MAP_HOST: MAP_HOST,
    GOOGLE_MAP_PLACES_APIKEY: GOOGLE_MAP_PLACES_APIKEY,
    WEBSOCKET_URL: WEBSOCKET_URL,
    VECTOR_MAP_HOST: env.VECTOR_MAP_HOST,
    eas: {
      projectId: env.EAS_PROJECT_ID
    }
  },
  experiments: {
    tsconfigPaths: true
  },
  splash: {
    image: './assets/loading-screen.png',
    resizeMode: 'cover',
    backgroundColor: '#ffffff'
  },
  androidStatusBar: {
    backgroundColor: '#00000000',
    translucent: true,
    barStyle: 'dark-content'
  },
  notification: {
    icon: './assets/notification-android-icon.png',
    color: '#FFFFFF'
  },
  updates: {
    url: 'https://u.expo.dev/c31c6828-3c32-4c7a-aabc-f9b8336b3b66'
  },
  platforms: ['ios', 'android'],
  assetBundlePatterns: ['**/*', 'assets/db/*.db'],
  ios: {
    supportsTablet: false,
    bundleIdentifier: env.PACKAGE_NAME_IOS, // com.nomadmania.app2
    config: {
      googleMapsApiKey: env.IOS_GOOGLE_MAP_APIKEY
    },
    infoPlist: {
      UIBackgroundModes: ['fetch'],
      NSLocationAlwaysUsageDescription:
        'Turn on location service to allow NomadMania.com find friends nearby.',
      NSPhotoLibraryUsageDescription:
        'Enable NomadMania.com to access your photo library to upload your profile picture. Any violence, excess of nudity, stolen picture, or scam is forbidden',
      NSPhotoLibraryAddUsageDescription:
        'Enable NomadMania.com to access your photo library to upload your profile picture. Any violence, excess of nudity, stolen picture, or scam is forbidden',
      NSPushNotificationsDescription:
        'This will allow NomadMania.com to send you notifications. Also you can disable it in app settings',
      NSDocumentsFolderUsageDescription:
        'Nomadmania app needs access to the documents folder to select files.',
      NSCameraUsageDescription: 'Nomadmania app needs access to the camera to record video.',
      NSLocationWhenInUseUsageDescription:
        'NomadMania app needs access to your location to show relevant data.'
    },
    privacyManifests: {
      NSPrivacyAccessedAPITypes: [
        {
          NSPrivacyAccessedAPIType: 'NSPrivacyAccessedAPICategoryUserDefaults',
          NSPrivacyAccessedAPITypeReasons: ['CA92.1']
        }
      ]
    }
  },
  android: {
    package: env.PACKAGE_NAME_ANDROID, // com.nomadmania.presentation
    config: {
      googleMaps: {
        apiKey: env.ANDROID_GOOGLE_MAP_APIKEY
      }
    },
    googleServicesFile: './google-services.json',
    permissions: [
      // 'ACCESS_BACKGROUND_LOCATION',
      'ACCESS_FINE_LOCATION',
      'ACCESS_COARSE_LOCATION',
      'READ_EXTERNAL_STORAGE',
      'WRITE_EXTERNAL_STORAGE',
      'NOTIFICATIONS',
      'USER_FACING_NOTIFICATIONS',
      'INTERNET',
      'CAMERA',
      'MODIFY_AUDIO_SETTINGS'
    ],
    versionCode: 77 // next version submitted to Google Play needs to be higher than that 2.0.24
  },
  plugins: [
    [
      'expo-image-picker',
      {
        photosPermission: 'Allow NomadMania.com access to your photo library to upload photos.',
        cameraPermission: 'Allow NomadMania.com access to your camera to upload photos directly.'
      }
    ],
    [
      'expo-build-properties',
      {
        android: {
          minSdkVersion: 24,
          targetSdkVersion: 34
          // kotlinVersion: '1.7.1'
        }
      }
    ],
    [
      '@sentry/react-native/expo',
      {
        organization: env.SENTRY_ORG,
        project: env.SENTRY_PROJECT,
        url: 'https://sentry.io/'
      }
    ],
    [
      'expo-asset',
      {
        assets: [
          './assets/db/nmRegions.db',
          './assets/db/darePlaces.db',
          './assets/db/nmCountries.db'
        ]
      }
    ],
    'expo-font',
    [
      'expo-av',
      {
        microphonePermission: 'Allow Nomadmania to access your microphone.'
      }
    ],
    ['@maplibre/maplibre-react-native']
  ]
});
