<template>
  <div class="rounded-xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-gray-900">
    <div class="mb-3 flex items-center justify-between">
      <h2 class="text-sm font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Summary</h2>
      <div class="flex items-center gap-2">
        <button
          v-if="activeFilter"
          @click="emit('filter', null)"
          class="text-xs text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
        >
          Show all
        </button>
        <span class="text-sm text-gray-500 dark:text-gray-400">{{ total }} checks</span>
      </div>
    </div>

    <!-- Bar -->
    <div class="mb-4 flex h-3 overflow-hidden rounded-full bg-gray-100 dark:bg-gray-800" v-if="total > 0">
      <div
        v-if="summary.pass > 0"
        class="bg-green-500 cursor-pointer transition-all hover:brightness-110"
        :style="{ width: pct(summary.pass) }"
        @click="emit('filter', 'pass')"
      ></div>
      <div
        v-if="summary.info > 0"
        class="bg-blue-500 cursor-pointer transition-all hover:brightness-110"
        :style="{ width: pct(summary.info) }"
        @click="emit('filter', 'info')"
      ></div>
      <div
        v-if="summary.warn > 0"
        class="bg-yellow-500 cursor-pointer transition-all hover:brightness-110"
        :style="{ width: pct(summary.warn) }"
        @click="emit('filter', 'warn')"
      ></div>
      <div
        v-if="summary.fail > 0"
        class="bg-red-500 cursor-pointer transition-all hover:brightness-110"
        :style="{ width: pct(summary.fail) }"
        @click="emit('filter', 'fail')"
      ></div>
    </div>

    <!-- Counts (clickable) -->
    <div class="flex flex-wrap gap-4 text-sm">
      <button
        v-if="summary.fail > 0"
        @click="emit('filter', activeFilter === 'fail' ? null : 'fail')"
        class="flex items-center gap-1.5 rounded-md px-2 py-0.5 transition"
        :class="activeFilter === 'fail' ? 'bg-red-100 dark:bg-red-900/30' : 'hover:bg-gray-100 dark:hover:bg-gray-800'"
      >
        <span class="inline-block h-3 w-3 rounded-full bg-red-500"></span>
        <span class="font-medium text-gray-700 dark:text-gray-300">{{ summary.fail }} failed</span>
      </button>
      <button
        v-if="summary.warn > 0"
        @click="emit('filter', activeFilter === 'warn' ? null : 'warn')"
        class="flex items-center gap-1.5 rounded-md px-2 py-0.5 transition"
        :class="activeFilter === 'warn' ? 'bg-yellow-100 dark:bg-yellow-900/30' : 'hover:bg-gray-100 dark:hover:bg-gray-800'"
      >
        <span class="inline-block h-3 w-3 rounded-full bg-yellow-500"></span>
        <span class="font-medium text-gray-700 dark:text-gray-300">{{ summary.warn }} warnings</span>
      </button>
      <button
        @click="emit('filter', activeFilter === 'pass' ? null : 'pass')"
        class="flex items-center gap-1.5 rounded-md px-2 py-0.5 transition"
        :class="activeFilter === 'pass' ? 'bg-green-100 dark:bg-green-900/30' : 'hover:bg-gray-100 dark:hover:bg-gray-800'"
      >
        <span class="inline-block h-3 w-3 rounded-full bg-green-500"></span>
        <span class="font-medium text-gray-700 dark:text-gray-300">{{ summary.pass }} passed</span>
      </button>
      <button
        v-if="summary.info > 0"
        @click="emit('filter', activeFilter === 'info' ? null : 'info')"
        class="flex items-center gap-1.5 rounded-md px-2 py-0.5 transition"
        :class="activeFilter === 'info' ? 'bg-blue-100 dark:bg-blue-900/30' : 'hover:bg-gray-100 dark:hover:bg-gray-800'"
      >
        <span class="inline-block h-3 w-3 rounded-full bg-blue-500"></span>
        <span class="font-medium text-gray-700 dark:text-gray-300">{{ summary.info }} info</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Summary, CheckStatus } from '~/types/dns'

const props = defineProps<{
  summary: Summary
  activeFilter?: CheckStatus | null
}>()

const emit = defineEmits<{
  filter: [status: CheckStatus | null]
}>()

const total = computed(() => props.summary.pass + props.summary.warn + props.summary.fail + props.summary.info)

function pct(count: number): string {
  if (total.value === 0) return '0%'
  return `${(count / total.value * 100).toFixed(1)}%`
}
</script>
