<!-- #region HealthMatrix [C:3] [TYPE Component] [SEMANTICS health, matrix, dashboard, summary, filter] -->
<!-- @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-gray-100 rounded-xl"></div>
      {/each}
    </div>
  {:else if error}
    <div class="p-4 bg-red-50 border border-red-200 rounded-xl text-red-700">
      <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-indigo-400 bg-indigo-50 shadow-sm' : 'border-gray-200 bg-white hover:border-gray-300 hover:shadow-sm'}"
      >
        <div class="text-3xl font-bold {selectedFilter === '' ? 'text-indigo-700' : 'text-gray-900'}">{all_count}</div>
        <div class="mt-1 text-xs font-semibold uppercase tracking-wider {selectedFilter === '' ? 'text-indigo-600' : 'text-gray-500'}">{$t.health?.all_dashboards || 'ALL'}</div>
        <div class="mt-0.5 text-[10px] {selectedFilter === '' ? 'text-indigo-400' : 'text-gray-400'}">{$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-green-500 bg-green-50 shadow-sm ring-1 ring-green-500' : 'border-green-200 bg-green-50/40 hover:bg-green-50 hover:border-green-300'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-green-600" 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-green-700">{pass_count}</div>
        <div class="mt-1 text-xs font-semibold text-green-600 uppercase tracking-wider">{$t.health?.status_pass || 'PASS'}</div>
        <div class="mt-0.5 text-[10px] text-green-500/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-yellow-50 shadow-sm ring-1 ring-yellow-500' : 'border-yellow-200 bg-yellow-50/40 hover:bg-yellow-50 hover:border-yellow-300'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-yellow-600" 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-yellow-700">{warn_count}</div>
        <div class="mt-1 text-xs font-semibold text-yellow-600 uppercase tracking-wider">{$t.health?.status_warn || 'WARN'}</div>
        <div class="mt-0.5 text-[10px] text-yellow-500/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-red-500 bg-red-50 shadow-sm ring-1 ring-red-500' : 'border-red-200 bg-red-50/40 hover:bg-red-50 hover:border-red-300'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-red-600" 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-red-700">{fail_count}</div>
        <div class="mt-1 text-xs font-semibold text-red-600 uppercase tracking-wider">{$t.health?.status_fail || 'FAIL'}</div>
        <div class="mt-0.5 text-[10px] text-red-500/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-gray-500 bg-gray-100 shadow-sm ring-1 ring-gray-500' : 'border-gray-200 bg-gray-50/40 hover:bg-gray-50 hover:border-gray-300'}"
      >
        <div class="absolute top-2 right-2 opacity-10">
          <svg class="w-12 h-12 text-gray-400" 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-gray-500">{unknown_count}</div>
        <div class="mt-1 text-xs font-semibold text-gray-500 uppercase tracking-wider">{$t.health?.status_unknown || 'UNKNOWN'}</div>
        <div class="mt-0.5 text-[10px] text-gray-400/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-gray-200 rounded-xl text-gray-400">
        <svg class="w-10 h-10 mx-auto mb-2 text-gray-300" 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 -->
