/**
 * EnvBadge — Environment-Pille im Header
 *
 * Zeigt PROD / STAGING / DEV in Petrol-Tint (analog DCT Header-Badge).
 *
 * Aufruf:
 *   <EnvBadge env={process.env.NEXT_PUBLIC_VERCEL_ENV} />
 *   <EnvBadge env="production" />
 *
 * Brand-Verbesserung vs DCT: 3-stufige Tone-Differenzierung
 *   - PROD: primary-tint (sticht im Petrol-dark-Header hervor)
 *   - STAGING: status-yellow (Warnung "kein Echtbetrieb")
 *   - DEV: status-grey (neutral, ignorierbar)
 *
 * KAR-521 Stage 4
 */

export type EnvKey = "prod" | "staging" | "dev"

export const ENV_LABEL: Record<EnvKey, string> = {
  prod: "PROD",
  staging: "STAGING",
  dev: "DEV",
}

export const ENV_TONE_CLASSES: Record<EnvKey, string> = {
  prod: "bg-primary-tint text-primary-dark",
  staging: "bg-status-yellow text-foreground",
  dev: "bg-status-grey text-foreground",
}

export function resolveEnv(value: string | undefined): EnvKey {
  const v = (value ?? "").toLowerCase()
  if (v === "production" || v === "prod") return "prod"
  if (v === "preview" || v === "staging") return "staging"
  if (v === "development" || v === "dev") return "dev"
  return "dev"
}

interface Props {
  env?: string
  className?: string
}

export default function EnvBadge({ env, className }: Props) {
  const key = resolveEnv(env)
  const tone = ENV_TONE_CLASSES[key]
  const cls = `inline-flex items-center px-2 py-0.5 rounded-sm text-[10px] font-semibold tracking-wider ${tone}${className ? " " + className : ""}`
  return <span className={cls}>{ENV_LABEL[key]}</span>
}
