<script lang="ts">
  import { onMount } from 'svelte';

  // Reads /healthz on mount and surfaces the deployed server version
  // as understated text. Useful when verifying which image a container
  // is actually running (came up debugging the in-app update flow when
  // it wasn't obvious whether v2026.05.10.0 or .1 was deployed).
  //
  // /healthz is unauthenticated, so the bare fetch works without
  // credentials. Renders nothing on parse failure or pre-version
  // images that don't include the field — graceful degradation.

  type Health = { status: string; version?: string };

  let version = $state<string | null>(null);

  onMount(async () => {
    try {
      const res = await fetch('/healthz');
      if (!res.ok) return;
      const body = (await res.json()) as Partial<Health>;
      if (body.version && body.version !== 'dev') {
        version = body.version;
      } else if (body.version === 'dev') {
        // Local dev images report "dev" — show it so the operator
        // can tell they're not on a release tag.
        version = 'dev';
      }
    } catch {
      // network / parse error — silent.
    }
  });
</script>

{#if version}
  <p class="text-xs text-text-secondary">Server {version}</p>
{/if}
