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

import type { ConfigContext, ExpoConfig } from 'expo/config';
const { DEEP_LINK_PATHS } = require('./config/deepLinks');
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 NM_API_HOST = env.NM_API_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')
});

const intentData = DEEP_LINK_PATHS.map((pathPrefix: string) => ({
  scheme: 'https',
  host: 'nomadmania.com',
  pathPrefix,
}));

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.53',
  // Should be updated after every dependency change
  runtimeVersion: '1.7',
  orientation: 'portrait',
  icon: './assets/icon-logo.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/splash-screen-world.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: intentData,
        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'
    ],
    splash: {
      backgroundColor: '#ffffff',
      imageWidth: 200,
      image: './assets/icon-logo.png',
      resizeMode: 'contain'
    },
    versionCode: 115 // 2.0.53, last version sent to Google is 114 (2.0.52)
  },
  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',
      {
        ios: {
          newArchEnabled: true,
          deploymentTarget: '15.1'
        },
        android: {
          minSdkVersion: 24,
          targetSdkVersion: 36
          // 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-audio',
      {
        microphonePermission: 'Allow Nomadmania to access your microphone.'
      }
    ],
    [
      'expo-video',
      {
        supportsBackgroundPlayback: true,
        supportsPictureInPicture: true
      }
    ],
    ['@maplibre/maplibre-react-native'],
    [
      'expo-location',
      {
        isIosBackgroundLocationEnabled: true,
        isAndroidBackgroundLocationEnabled: false
      }
    ],
    'react-native-compressor'
  ]
});
