import { Flag } from "@saeeol/core/flag/flag"
import { InstallationChannel, InstallationVersion } from "@saeeol/core/installation/version"

export type Backend = "effect-httpapi" | "hono"

export type Selection = {
  backend: Backend
  reason: "env" | "stable" | "explicit"
}

export type Attributes = ReturnType<typeof attributes>

export function select(): Selection {
  if (Flag.SAEEOL_EXPERIMENTAL_HTTPAPI) return { backend: "effect-httpapi", reason: "env" }
  return { backend: "hono", reason: "stable" }
}

export function attributes(selection: Selection): Record<string, string> {
  return {
    "saeeol.server.backend": selection.backend,
    "saeeol.server.backend.reason": selection.reason,
    "saeeol.installation.channel": InstallationChannel,
    "saeeol.installation.version": InstallationVersion,
  }
}

export function force(selection: Selection, backend: Backend): Selection {
  return {
    backend,
    reason: selection.backend === backend ? selection.reason : "explicit",
  }
}
