/**
 * HTTP request helpers (server-only).
 *
 * Thin wrappers around `next/headers` used for best-effort client
 * identification (rate-limit keying, logging). These values are NOT
 * stored as audit-trail identifiers.
 */

/**
 * Resolve the client IP from request headers (best-effort, used for
 * rate-limit keying only — not stored).
 *
 * Falls back to `"unknown"` when headers are unavailable (e.g. during
 * build or in non-request contexts).
 */
export const resolveClientIp = async (): Promise<string> => {
  try {
    const { headers } = await import("next/headers")
    const headerList = await headers()
    return (
      headerList.get("x-forwarded-for")?.split(",")[0]?.trim() ??
      headerList.get("x-real-ip") ??
      "unknown"
    )
  } catch {
    return "unknown"
  }
}

/**
 * Resolve the User-Agent string from request headers (best-effort).
 *
 * Falls back to `"unknown"` when headers are unavailable (e.g. during
 * build or in non-request contexts).
 */
export const getUserAgent = async (): Promise<string> => {
  try {
    const { headers } = await import("next/headers")
    const headerList = await headers()
    return headerList.get("user-agent") ?? "unknown"
  } catch {
    return "unknown"
  }
}
