<!-- #region HealthMatrix [C:3] [TYPE Component] [SEMANTICS health, matrix, dashboard, summary, filter] -->
<!-- @ingroup Components -->
<!-- @BRIEF Clickable health status summary grid — clicking a card emits a filter event. -->
<!-- @LAYER UI -->
<!-- @RELATION BINDS_TO -> [EXT:frontend:i18n] -->
<!-- @UX_STATE Idle -> 4 clickable status cards. -->
<!-- @UX_STATE Loading -> Skeleton cards. -->
<!-- @UX_STATE Error -> Error message. -->
<!-- @UX_STATE Active -> One card highlighted (the current filter). -->
<script lang="ts">
  import { t } from '$lib/i18n/index.svelte.js';

  let {
    pass_count = 0,
    warn_count = 0,
    fail_count = 0,
    unknown_count = 0,
    loading = false,
    error = null,
    selectedFilter = '',
    onSelect = (_status: string) => {},
  } = $props();

  let total = $derived(pass_count + warn_count + fail_count + unknown_count);
  const all_count = $derived(total);
</script>

<div class="health-matrix-summary">
  {#if loading}
    <div class="grid grid-cols-2 md:grid-cols-5 gap-3 animate-pulse">
      {#each Array(5) as _, i (i)}
        <div class="h-24 bg-surface-muted rounded-xl"></div>
      {/each}
    </div>
  {:else if error}
    <div class="p-4 bg-destructive-light border border-destructive-ring rounded-xl text-destructive">
      <p class="font-semibold">{$t.health?.load_failed || 'Failed to load health data'}</p>
      <p class="text-sm mt-1">{error}</p>
    </div>
  {:else}
    <div class="grid grid-cols-2 md:grid-cols-5 gap-3">
      <!-- ALL -->
      <button
        onclick={() => onSelect('')}
        class="relative p-4 rounded-xl border-2 text-left transition-all cursor-pointer
          {selectedFilter === '' ? 'border-primary-ring bg-primary-light shadow-sm' : 'border-border bg-surface-card hover:border-border-strong hover:shadow-sm'}"
      >
        <div class="text-3xl font-bold {selectedFilter === '' ? 'text-primary' : 'text-text'}">{all_count}</div>
        <div class="mt-1 text-xs font-semibold uppercase tracking-wider {selectedFilter === '' ? 'text-primary' : 'text-text-muted'}">{$t.health?.all_dashboards || 'ALL'}</div>
        <div class="mt-0.5 text-[10px] {selectedFilter === '' ? 'text-primary' : 'text-text-subtle'}">{$t.health?.total_dashboards || 'Total'}</div>
      </button>

      <!-- PASS -->
      <button
        onclick={() => onSelect(selectedFilter === 'PASS' ? '' : 'PASS')}
        class="relative p-4 rounded-xl border-2 text-left transition-all cursor-pointer
          {selectedFilter === 'PASS' ? 'border-success bg-success-light shadow-sm ring-1 ring-success-ring' : 'border-success bg-success-light/40 hover:bg-success-light hover:border-success'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-success" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
        </div>
        <div class="text-3xl font-bold text-success">{pass_count}</div>
        <div class="mt-1 text-xs font-semibold text-success uppercase tracking-wider">{$t.health?.status_pass || 'PASS'}</div>
        <div class="mt-0.5 text-[10px] text-success/70">{$t.health?.healthy_dashboards || 'Healthy'}</div>
      </button>

      <!-- WARN -->
      <button
        onclick={() => onSelect(selectedFilter === 'WARN' ? '' : 'WARN')}
        class="relative p-4 rounded-xl border-2 text-left transition-all cursor-pointer
          {selectedFilter === 'WARN' ? 'border-yellow-500 bg-warning-light shadow-sm ring-1 ring-yellow-500' : 'border-warning bg-warning-light/40 hover:bg-warning-light hover:border-warning'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-warning" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
        </div>
        <div class="text-3xl font-bold text-warning">{warn_count}</div>
        <div class="mt-1 text-xs font-semibold text-warning uppercase tracking-wider">{$t.health?.status_warn || 'WARN'}</div>
        <div class="mt-0.5 text-[10px] text-warning/70">{$t.health?.warning_dashboards || 'Needs attention'}</div>
      </button>

      <!-- FAIL -->
      <button
        onclick={() => onSelect(selectedFilter === 'FAIL' ? '' : 'FAIL')}
        class="relative p-4 rounded-xl border-2 text-left transition-all cursor-pointer
          {selectedFilter === 'FAIL' ? 'border-destructive-ring bg-destructive-light shadow-sm ring-1 ring-destructive-ring' : 'border-destructive-ring bg-destructive-light/40 hover:bg-destructive-light hover:border-destructive-ring'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-destructive" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6 18L18 6M6 6l12 12" />
          </svg>
        </div>
        <div class="text-3xl font-bold text-destructive">{fail_count}</div>
        <div class="mt-1 text-xs font-semibold text-destructive uppercase tracking-wider">{$t.health?.status_fail || 'FAIL'}</div>
        <div class="mt-0.5 text-[10px] text-destructive/70">{$t.health?.failing_dashboards || 'Requires action'}</div>
      </button>

      <!-- UNKNOWN -->
      <button
        onclick={() => onSelect(selectedFilter === 'UNKNOWN' ? '' : 'UNKNOWN')}
        class="relative p-4 rounded-xl border-2 text-left transition-all cursor-pointer
          {selectedFilter === 'UNKNOWN' ? 'border-border-strong bg-surface-muted shadow-sm ring-1 ring-secondary-ring' : 'border-border bg-surface-muted/40 hover:bg-surface-muted hover:border-border-strong'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-text-subtle" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
        </div>
        <div class="text-3xl font-bold text-text-muted">{unknown_count}</div>
        <div class="mt-1 text-xs font-semibold text-text-muted uppercase tracking-wider">{$t.health?.status_unknown || 'UNKNOWN'}</div>
        <div class="mt-0.5 text-[10px] text-text-subtle/70">{$t.health?.unknown_dashboards || 'No data'}</div>
      </button>
    </div>

    {#if total === 0}
      <div class="mt-4 p-8 text-center border-2 border-dashed border-border rounded-xl text-text-subtle">
        <svg class="w-10 h-10 mx-auto mb-2 text-text-subtle" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
        </svg>
        <p>{$t.health?.no_records_for_environment || 'No validation records for this environment'}</p>
      </div>
    {/if}
  {/if}
</div>
<!-- #endregion HealthMatrix -->
