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

type AppVariant = 'development' | 'preview' | 'production';

const VARIANT_CONFIG: Record<
  AppVariant,
  {
    readonly appName: string;
    readonly scheme: string;
    readonly iosBundleIdentifier: string;
    readonly androidPackage: string;
  }
> = {
  development: {
    appName: 'Learnitive Dev',
    scheme: 'interactive-dev',
    iosBundleIdentifier: 'com.learnitive.app.dev',
    androidPackage: 'com.learnitive.app.dev',
  },
  preview: {
    appName: 'Learnitive Preview',
    scheme: 'interactive-preview',
    iosBundleIdentifier: 'com.learnitive.app.preview',
    androidPackage: 'com.learnitive.app.preview',
  },
  production: {
    appName: 'Learnitive',
    scheme: 'learnitive',
    iosBundleIdentifier: 'com.learnitive.app',
    androidPackage: 'com.learnitive.app',
  },
};

function resolveAppVariant(value: string | undefined): AppVariant {
  switch (value) {
    case 'development':
    case 'preview':
    case 'production':
      return value;
    default:
      return 'production';
  }
}

const APP_VARIANT = resolveAppVariant(process.env['APP_VARIANT']);
const variant = VARIANT_CONFIG[APP_VARIANT];

// URL the WebView shell loads. Point this at a LAN Vite dev server
// (e.g. http://192.168.1.20:5173) for a live dev loop against local code.
const LEARNITIVE_WEB_URL =
  process.env['EXPO_PUBLIC_LEARNITIVE_WEB_URL'] ??
  'https://www.learnitive.com';

// Set by `eas init` / the EAS dashboard. Required for EAS builds only;
// local `expo start` works without it.
const EAS_PROJECT_ID = process.env['EAS_PROJECT_ID'];

const config: ExpoConfig = {
  name: variant.appName,
  slug: 'learnitive',
  platforms: ['ios', 'android'],
  scheme: variant.scheme,
  version: '0.1.0',
  runtimeVersion: {
    policy: 'appVersion',
  },
  orientation: 'default',
  userInterfaceStyle: 'automatic',
  ios: {
    supportsTablet: true,
    bundleIdentifier: variant.iosBundleIdentifier,
    infoPlist: {
      ITSAppUsesNonExemptEncryption: false,
    },
  },
  android: {
    package: variant.androidPackage,
  },
  extra: {
    appVariant: APP_VARIANT,
    learnitiveWebUrl: LEARNITIVE_WEB_URL,
    ...(EAS_PROJECT_ID ? { eas: { projectId: EAS_PROJECT_ID } } : {}),
  },
};

export default config;
