<template>
  <div :class="['network-card', statusClass]" @click="goToList">
    <div class="card-icon">{{ icon }}</div>
    <div class="card-title">{{ title }}</div>
    <div class="card-status" v-if="status && status.last_checked_at">
      <span class="status-icon">{{ status.is_healthy ? '✅' : '❌' }}</span>
      <span class="status-text">{{ status.is_healthy ? '정상' : '오류' }}</span>
    </div>
    <div class="card-status" v-else>
      <span class="status-icon">⚪</span>
      <span class="status-text">대기</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;
  align-items: center;
  padding: 16px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}
.network-card:hover {
  background: var(--bg-tertiary, #f8fafc);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
.network-card.healthy { border-top: 4px solid #22c55e; }
.network-card.unhealthy { border-top: 4px solid #ef4444; }
.network-card.pending { border-top: 4px solid #9ca3af; }

.card-icon { font-size: 28px; margin-bottom: 8px; }
.card-title { font-size: 14px; font-weight: 600; color: var(--text-primary); margin-bottom: 10px; }

.card-status { display: flex; align-items: center; justify-content: center; gap: 6px; margin-bottom: 4px; }
.status-icon { font-size: 16px; }
.status-text { font-size: 15px; font-weight: 500; color: var(--text-secondary); }

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