import {defineConfig} from "hot-updater";
import {bare} from "@hot-updater/bare";
import {standaloneRepository} from "@hot-updater/standalone";
import {coreStaticStorage} from "./tools/core-static-storage.mjs";

const adminToken = process.env.DANOGO_MOBILE_OTA_ADMIN_TOKEN || "";

export default defineConfig({
  updateStrategy: "appVersion",
  compressStrategy: "zip",

  // Keep the first proving pass simple. Archive delivery remains the fallback
  // even when diff generation is enabled later.
  patch: {
    enabled: false,
  },

  // Hash verification is mandatory even before RSA signing is enabled.
  // RSA bundle signing will be turned on once the private key is stored in
  // DanoGo Source rather than source control.
  signing: {
    enabled: false,
  },

  build: bare({
    enableHermes: true,
    resetCache: true,
    outDir: "build/hot-updater",
  }),

  storage: () =>
    coreStaticStorage({
      rootDir:
        process.env.DANOGO_MOBILE_BUNDLE_DIR ||
        "../ota-server/data/bundles",
      publicBaseUrl:
        process.env.DANOGO_MOBILE_BUNDLE_PUBLIC_BASE ||
        "https://danogo.theappmasters.com/mobile-bundles",
    }),

  database: standaloneRepository({
    baseUrl:
      process.env.DANOGO_MOBILE_OTA_INTERNAL_URL ||
      "http://127.0.0.1:18081/mobile-updates",
    commonHeaders: adminToken
      ? {Authorization: `Bearer ${adminToken}`}
      : {},
  }),

  platform: {
    android: {
      androidManifestPaths: [
        "android/app/src/main/AndroidManifest.xml",
      ],
    },
    ios: {
      infoPlistPaths: [
        "ios/DanoGoMobile/Info.plist",
      ],
    },
  },

  nativeBuild: {
    android: {
      default: {
        packageName: "com.danogomobile",
        applicationId: "com.dano.go",
        variant: "Release",
        aab: false,
      },
    },
    ios: {
      default: {
        bundleIdentifier: "com.dano.go",
        scheme: "DanoGoMobile",
        configuration: "Release",
        installPods: true,
        destination: ["ios-device"],
      },
    },
  },
});
