<template>
  <div :class="['network-card', statusClass]" @click="goToList">
    <div class="card-row">
      <span class="card-title">{{ icon }} {{ title }}</span>
      <span class="status-badge">{{ status?.is_healthy ? '✅' : status?.last_checked_at ? '❌' : '⚪' }}</span>
    </div>
    <div class="card-time" v-if="status && status.last_checked_at">
      {{ formatTimeAgo(status.last_checked_at) }}
    </div>
  </div>
</template>

<script setup lang="ts">
const props = defineProps<{
  type: 'pubnet' | 'privnet'
  title: string
  icon: string
  status: {
    is_healthy: number
    last_checked_at: string | null
    last_target_name: string | null
  } | null
}>()

const router = useRouter()

const statusClass = computed(() => {
  if (!props.status || !props.status.last_checked_at) return 'pending'
  return props.status.is_healthy ? 'healthy' : 'unhealthy'
})

function goToList() {
  router.push(`/network/${props.type}`)
}

function formatTimeAgo(datetime: string | null): string {
  if (!datetime) return '-'
  const now = new Date()
  const then = new Date(datetime.replace(' ', 'T') + '+09:00')
  const diff = Math.floor((now.getTime() - then.getTime()) / 1000)
  if (diff < 60) return `${diff}초 전`
  if (diff < 3600) return `${Math.floor(diff / 60)}분 전`
  if (diff < 86400) return `${Math.floor(diff / 3600)}시간 전`
  return `${Math.floor(diff / 86400)}일 전`
}
</script>

<style scoped>
.network-card {
  display: flex;
  flex-direction: column;
  padding: 8px 10px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}
.network-card:hover {
  background: var(--bg-tertiary, #f8fafc);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.network-card.healthy { border-left: 3px solid #22c55e; }
.network-card.unhealthy { border-left: 3px solid #ef4444; }
.network-card.pending { border-left: 3px solid #9ca3af; }

.card-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title { font-size: 12px; font-weight: 600; color: var(--text-primary); }
.status-badge { font-size: 12px; }

.card-time { font-size: 10px; color: var(--text-muted); margin-top: 2px; }
</style>
