type Auth = unknown

type Env = {
  SAEEOL_API_KEY?: string
  SAEEOL_ORG_ID?: string
}

type Provider = {
  key?: unknown
  options?: Record<string, unknown>
}

export type SaeeolIndexingAuth = {
  apiKey?: string
  baseUrl?: string
  organizationId?: string
}

const providers = [
  "openai",
  "ollama",
  "openai-compatible",
  "gemini",
  "mistral",
  "vercel-ai-gateway",
  "bedrock",
  "openrouter",
  "voyage",
]

function record(value: unknown): Record<string, unknown> {
  if (!value || typeof value !== "object" || Array.isArray(value)) return {}
  return value as Record<string, unknown>
}

function text(value: unknown): string | undefined {
  if (typeof value !== "string") return
  const trimmed = value.trim()
  return trimmed || undefined
}

function token(auth: Auth): string | undefined {
  const data = record(auth)
  if (data.type === "api") return text(data.key)
  if (data.type === "oauth") return text(data.access)
  return
}

function org(auth: Auth): string | undefined {
  const data = record(auth)
  if (data.type === "oauth") return text(data.accountId)
  return
}

function value(input: unknown): boolean {
  if (input === undefined || input === null) return false
  if (typeof input === "string") return input.trim().length > 0
  if (typeof input === "object") return Object.values(input).some(value)
  return true
}

function hasOtherProvider(indexing: unknown): boolean {
  const cfg = record(indexing)
  return providers.some((provider) => value(cfg[provider]))
}

export function resolveSaeeolIndexingAuth(input: {
  config?: unknown
  provider?: Provider
  auth?: Auth
  env?: Env
}): SaeeolIndexingAuth {
  const config = record(input.config)
  const options = record(record(config.provider).saeeol)
  const provider = input.provider ?? record(input.provider)
  const providerOptions = record(provider.options)
  const providerConfig = record(options.options)
  const saeeol = record(record(config.indexing).saeeol)
  const env = input.env ?? process.env

  return {
    apiKey:
      text(saeeol.apiKey) ??
      text(providerConfig.apiKey) ??
      token(input.auth) ??
      text(provider.key) ??
      text(providerOptions.saeeolToken) ??
      text(env.SAEEOL_API_KEY),
    baseUrl: text(saeeol.baseUrl) ?? text(providerConfig.baseURL) ?? text(providerConfig.baseUrl),
    organizationId:
      text(saeeol.organizationId) ??
      text(providerConfig.saeeolOrganizationId) ??
      org(input.auth) ??
      text(providerOptions.saeeolOrganizationId) ??
      text(env.SAEEOL_ORG_ID),
  }
}

export function hasSaeeolIndexingAuth(input: Parameters<typeof resolveSaeeolIndexingAuth>[0]): boolean {
  return !!resolveSaeeolIndexingAuth(input).apiKey
}

export function shouldDefaultIndexingToSaeeol(indexing: unknown, auth: SaeeolIndexingAuth): boolean {
  const cfg = record(indexing)
  if (cfg.provider !== undefined || !auth.apiKey) return false
  return !hasOtherProvider(cfg)
}

export function indexingWithSaeeolDefault(config: unknown, auth: SaeeolIndexingAuth) {
  const cfg = record(config)
  const indexing = cfg.indexing
  if (!shouldDefaultIndexingToSaeeol(indexing, auth)) return indexing
  return { ...record(indexing), provider: "saeeol" }
}
