<script lang="ts">
  import type { ToolCallResult } from '$lib/stores/chat'
  import LoaderCircleIcon from '@lucide/svelte/icons/loader-circle'
  import CheckIcon from '@lucide/svelte/icons/check'
  import XIcon from '@lucide/svelte/icons/x'

  let { tool }: { tool: ToolCallResult } = $props()

  const rows = $derived.by(() => {
    if (tool.type !== 'tool_result' || !tool.result) return null
    const data = (tool.result as any).data ?? tool.result
    return Array.isArray(data) ? data as any[] : null
  })

  const loading = $derived(tool.type === 'tool_use')
  const error = $derived(tool.type === 'tool_result' ? tool.error : undefined)
</script>

{#if loading}
  <div class="flex items-center gap-2 rounded-lg border bg-card px-3 py-2 text-xs" role="status" aria-label="Loading metrics">
    <LoaderCircleIcon class="size-3 shrink-0 animate-spin text-primary" aria-hidden="true" />
    <span class="font-medium">Metrics</span>
    <span class="animate-pulse text-muted-foreground">loading…</span>
  </div>
{:else if error}
  <div class="flex items-center gap-2 rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-xs" role="alert" aria-label="Metrics error">
    <XIcon class="size-3 shrink-0 text-destructive" aria-hidden="true" />
    <span class="font-medium">Metrics</span>
    <span class="text-destructive">{error}</span>
  </div>
{:else if rows && rows.length > 0}
  <div class="rounded-lg border bg-card text-xs" aria-label="Metrics: {rows.length} samples">
    <div class="flex items-center gap-2 px-3 py-1.5 border-b">
      <CheckIcon class="size-3 shrink-0 text-success" aria-hidden="true" />
      <span class="font-medium">{rows.length} sample{rows.length === 1 ? '' : 's'}</span>
    </div>
    <div class="max-h-48 overflow-y-auto">
      <table class="w-full">
        <thead>
          <tr class="border-b text-muted-foreground">
            <th class="px-2 py-1 text-left font-medium">Time</th>
            <th class="px-2 py-1 text-left font-medium">Metric</th>
            <th class="px-2 py-1 text-right font-medium">Avg</th>
            <th class="px-2 py-1 text-right font-medium">Min</th>
            <th class="px-2 py-1 text-right font-medium">Max</th>
          </tr>
        </thead>
        <tbody>
          {#each rows as row}
            <tr class="border-b last:border-0 hover:bg-muted/30">
              <td class="px-2 py-1 font-mono tabular-nums whitespace-nowrap">{row.bucket?.slice(11, 16) ?? row.bucket?.slice(0, 19) ?? '—'}</td>
              <td class="px-2 py-1 font-mono max-w-32 truncate">{row.metric ?? '—'}</td>
              <td class="px-2 py-1 font-mono tabular-nums text-right">{row.avg ?? '—'}</td>
              <td class="px-2 py-1 font-mono tabular-nums text-right text-muted-foreground">{row.min ?? '—'}</td>
              <td class="px-2 py-1 font-mono tabular-nums text-right text-muted-foreground">{row.max ?? '—'}</td>
            </tr>
          {/each}
        </tbody>
      </table>
    </div>
  </div>
{:else}
  <div class="flex items-center gap-2 rounded-lg border bg-card px-3 py-2 text-xs" role="status" aria-label="Metrics: no data">
    <CheckIcon class="size-3 shrink-0 text-success" aria-hidden="true" />
    <span class="font-medium">Metrics</span>
    <span class="text-muted-foreground">no data</span>
  </div>
{/if}
