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.38',
  // Should be updated after every dependency change
  runtimeVersion: '1.6',
  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
    },
    associatedDomains: ['applinks:nomadmania.com'],
    infoPlist: {
      UIBackgroundModes: ['location', 'fetch', 'remote-notification'],
      NSLocationAlwaysUsageDescription:
        'NomadMania uses your location in the background so other users see your latest location and regions are marked as visited automatically.',
      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 uses your location to show it to other users and to help mark regions as visited.',
      NSLocationAlwaysAndWhenInUseUsageDescription:
        'NomadMania uses your location in the background so other users see your latest location and regions are marked as visited automatically.',
      LSApplicationQueriesSchemes: ['comgooglemaps']
    },
    privacyManifests: {
      NSPrivacyAccessedAPITypes: [
        {
          NSPrivacyAccessedAPIType: 'NSPrivacyAccessedAPICategoryUserDefaults',
          NSPrivacyAccessedAPITypeReasons: ['CA92.1']
        }
      ]
    }
  },
  android: {
    package: env.PACKAGE_NAME_ANDROID, // com.nomadmania.presentation
    config: {
      googleMaps: {
        apiKey: env.ANDROID_GOOGLE_MAP_APIKEY
      }
    },
    intentFilters: [
      {
        action: 'VIEW',
        data: [
          {
            scheme: 'https',
            host: 'nomadmania.com',
            pathPrefix: '/profile/'
          },
          {
            scheme: 'https',
            host: 'nomadmania.com',
            pathPrefix: '/event/'
          },
          {
            scheme: 'https',
            host: 'nomadmania.com',
            pathPrefix: '/map/'
          }
        ],
        category: ['BROWSABLE', 'DEFAULT']
      }
    ],
    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',
      'READ_MEDIA_VIDEO'
    ],
    versionCode: 96 // 2.0.38, last version sent to Google is 95 (2.0.37)
  },
  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,
        note: 'Use SENTRY_AUTH_TOKEN env to authenticate with Sentry.',
        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'],
    [
      'expo-location',
      {
        isIosBackgroundLocationEnabled: true,
        isAndroidBackgroundLocationEnabled: false
      }
    ]
  ]
});