import type { ApiOrgDeployLocked, ApiOrgEnvVar } from "@/lib/api/admin-org-types"

export function isDeployFieldLocked(
  deployLocked: ApiOrgDeployLocked | undefined,
  section: string,
  field: string
): boolean {
  const block = deployLocked?.[section]
  if (!block?.locked) return false
  if (!block.fields?.length) return true
  return block.fields.includes(field)
}

export function isPluginDeployLocked(
  deployLocked: ApiOrgDeployLocked | undefined,
  pluginId: string
): boolean {
  return isDeployFieldLocked(deployLocked, "plugins", pluginId)
}

const GROUP_LABELS: Record<string, string> = {
  authentik: "Authentik / OIDC",
  nextcloud: "Nextcloud",
  onlyoffice: "OnlyOffice",
  ai_assistant: "UltiAI",
  search: "Recherche",
  immich: "Immich",
  jitsi: "Jitsi Meet",
  storage: "Stockage objet",
}

export function envGroupLabel(group: string): string {
  return GROUP_LABELS[group] ?? group
}

export function groupEnvVars(vars: ApiOrgEnvVar[]): Record<string, ApiOrgEnvVar[]> {
  const grouped: Record<string, ApiOrgEnvVar[]> = {}
  for (const v of vars) {
    const key = v.group || "other"
    grouped[key] = grouped[key] ?? []
    grouped[key].push(v)
  }
  return grouped
}
