global.modjoSentryConfig = {
  package: require("../package.json"),
  options: {},
}
require("common/sentry/instrument")

const modjo = require("@modjo/core")

const customConfig = require(`~/config`)

modjo({
  plugins: {
    // app: "express",
    config: {
      context: (ctx) => {
        ctx.set("customConfig", customConfig)
      },
    },
    oa: {
      pluginName: "oa",
      dependencies: {
        sentry: {},
        postgres: {},
        amqp: {},
        redisQueueDedup: {
          pluginName: "ioredis",
          context: (ctx) => {
            const redisConfig = {
              host: process.env.REDIS_QUEUE_DEDUP_HOST,
              port: process.env.REDIS_QUEUE_DEDUP_PORT || "6379",
              username: process.env.REDIS_QUEUE_DEDUP_USERNAME || "default",
              password: process.env.REDIS_QUEUE_DEDUP_PASSWORD,
              db: process.env.REDIS_QUEUE_DEDUP_DB || "0",
            }

            // Add Sentinel configuration if provided
            if (
              process.env.REDIS_QUEUE_DEDUP_SENTINELS &&
              process.env.REDIS_QUEUE_DEDUP_SENTINEL_MASTER
            ) {
              const sentinelEndpoints =
                process.env.REDIS_QUEUE_DEDUP_SENTINELS.split(",")
              redisConfig.sentinel = {
                sentinels: sentinelEndpoints.map((endpoint) => {
                  const [host, port] = endpoint.trim().split(":")
                  return { host, port: parseInt(port, 10) || 26379 }
                }),
                name: process.env.REDIS_QUEUE_DEDUP_SENTINEL_MASTER,
              }
            }

            ctx.set("config", {
              ...ctx.get("config"),
              redis: redisConfig,
            })
          },
        },
        redisHotGeodata: {
          pluginName: "ioredis",
          context: (ctx) => {
            const redisConfig = {
              host: process.env.REDIS_HOT_GEODATA_HOST,
              port: process.env.REDIS_HOT_GEODATA_PORT || "6379",
              username: process.env.REDIS_HOT_GEODATA_USERNAME || "default",
              password: process.env.REDIS_HOT_GEODATA_PASSWORD,
              db: process.env.REDIS_HOT_GEODATA_DB || "0",
            }

            // Add Sentinel configuration if provided
            if (
              process.env.REDIS_HOT_GEODATA_SENTINELS &&
              process.env.REDIS_HOT_GEODATA_SENTINEL_MASTER
            ) {
              const sentinelEndpoints =
                process.env.REDIS_HOT_GEODATA_SENTINELS.split(",")
              redisConfig.sentinel = {
                sentinels: sentinelEndpoints.map((endpoint) => {
                  const [host, port] = endpoint.trim().split(":")
                  return { host, port: parseInt(port, 10) || 26379 }
                }),
                name: process.env.REDIS_HOT_GEODATA_SENTINEL_MASTER,
              }
            }

            ctx.set("config", {
              ...ctx.get("config"),
              redis: redisConfig,
            })
          },
        },
      },
      buildParams: {
        apiPath: "api",
        sharedApiPath: "../../../libs/common/oapi",
      },
    },
  },
  dependencies: {
    // amqp: {},
    oapi: {
      pluginName: "microservice-oapi",
      dependencies: {
        oaGraphql: {
          pluginName: "oa-graphql",
        },
        hasura: {
          pluginName: "hasura",
        },
      },
    },
  },
})
