// Stable per-device identifier used to add forensic context to audit logs.
// Persisted in localStorage so the same browser/device reuses it across sessions.
// NOTE: this identifies a device/browser, not a human — on a shared kiosk every
// user of the same browser shares one id (intended: it distinguishes devices).

const STORAGE_KEY = 'sirh-device-id'
let cached: string | null = null

export const useDeviceId = (): string | null => {
  if (!import.meta.client) {
    return null
  }
  if (cached) {
    return cached
  }
  try {
    let id = localStorage.getItem(STORAGE_KEY)
    if (!id) {
      id = crypto.randomUUID()
      localStorage.setItem(STORAGE_KEY, id)
    }
    cached = id
    return id
  } catch {
    // localStorage unavailable (private mode, disabled) — degrade gracefully.
    return null
  }
}
