<script setup lang="ts">
import type { Client } from '~/types'

const props = defineProps<{ client: Client }>()

const daysAgo = (iso: string): number => {
  const diffMs = Date.now() - new Date(iso).getTime()
  return Math.max(0, Math.floor(diffMs / 86_400_000))
}

const info = computed(() => {
  const count = props.client.history_count
  if (count == null || count === 0) return null

  const updates = `${count} Verlauf-Update${count > 1 ? 's' : ''}`
  const last = props.client.last_history_at
  if (!last) return updates

  const d = daysAgo(last)
  const when =
    d === 0 ? 'heute' : `vor ${d} ${d === 1 ? 'Tag' : 'Tagen'}`
  return `${updates} · zuletzt ${when}`
})
</script>

<template>
  <NuxtLink
    :to="`/clients/${client.id}`"
    class="block border border-gray-200 bg-white px-4 py-3 hover:bg-blue-50 cursor-pointer"
  >
    <div class="flex items-baseline gap-4">
      <span class="text-sm font-mono text-gray-500 w-20 shrink-0">{{ client.client_number }}</span>
      <span class="text-sm font-medium text-gray-900">{{ client.name }}</span>
    </div>
    <p v-if="info" class="mt-1 pl-24 text-xs text-gray-400">{{ info }}</p>
  </NuxtLink>
</template>
