import { events, db as dbUtils } from "@devlego/backend-core"
import {
  Config,
  isSMTPConfig,
  isGoogleConfig,
  isOIDCConfig,
  isSettingsConfig,
} from "@devlego/types"
import env from "./../../../../environment"

const getConfigs = async (globalDb: any): Promise<Config[]> => {
  const response = await globalDb.allDocs(
    dbUtils.getConfigParams(
      {},
      {
        include_docs: true,
      }
    )
  )
  return response.rows.map((row: any) => row.doc)
}

export const backfill = async (
  globalDb: any,
  timestamp: string | number | undefined
) => {
  const configs = await getConfigs(globalDb)

  for (const config of configs) {
    if (isSMTPConfig(config)) {
      await events.email.SMTPCreated(timestamp)
    }
    if (isGoogleConfig(config)) {
      await events.auth.SSOCreated("google", timestamp)
      if (config.config.activated) {
        await events.auth.SSOActivated("google", timestamp)
      }
    }
    if (isOIDCConfig(config)) {
      await events.auth.SSOCreated("oidc", timestamp)
      if (config.config.configs[0].activated) {
        await events.auth.SSOActivated("oidc", timestamp)
      }
    }
    if (isSettingsConfig(config)) {
      const company = config.config.company
      if (company && company !== "Budibase") {
        await events.org.nameUpdated(timestamp)
      }

      const logoUrl = config.config.logoUrl
      if (logoUrl) {
        await events.org.logoUpdated(timestamp)
      }

      const platformUrl = config.config.platformUrl
      if (
        platformUrl &&
        platformUrl !== "http://localhost:10000" &&
        env.SELF_HOSTED
      ) {
        await events.org.platformURLUpdated(timestamp)
      }
    }
  }
}
