<script setup lang="ts">
import { onMounted } from 'vue'
import { useWipeStore } from '@/stores/wipe'
import { History, RefreshCw } from 'lucide-vue-next'

const wipeStore = useWipeStore()

function statusBadgeClass(status: string): string {
  switch (status) {
    case 'success': return 'bg-green-500/10 text-green-400'
    case 'failed':
    case 'rolled_back': return 'bg-red-500/10 text-red-400'
    case 'wiping':
    case 'pre_wipe':
    case 'post_wipe': return 'bg-yellow-500/10 text-yellow-400'
    default: return 'bg-neutral-700/50 text-neutral-400'
  }
}

function duration(start: string | null, end: string | null): string {
  if (!start || !end) return '\u2014'
  const ms = new Date(end).getTime() - new Date(start).getTime()
  const s = Math.floor(ms / 1000)
  const m = Math.floor(s / 60)
  if (m > 0) return `${m}m ${s % 60}s`
  return `${s}s`
}

onMounted(() => {
  wipeStore.fetchHistory()
})
</script>

<template>
  <div class="p-6 space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div class="flex items-center gap-3">
        <History class="w-5 h-5 text-oxide-500" />
        <div>
          <h1 class="text-2xl font-bold text-neutral-100">Wipe History</h1>
          <p class="text-sm text-neutral-500 mt-0.5">{{ wipeStore.history.length }} wipes recorded</p>
        </div>
      </div>
      <button
        @click="wipeStore.fetchHistory()"
        :disabled="wipeStore.isLoading"
        class="flex items-center gap-2 px-4 py-2 text-sm font-medium text-neutral-300 bg-neutral-800 hover:bg-neutral-700 disabled:opacity-50 rounded-lg transition-colors"
      >
        <RefreshCw class="w-4 h-4" :class="{ 'animate-spin': wipeStore.isLoading }" />
        Refresh
      </button>
    </div>

    <!-- History table -->
    <div class="bg-neutral-900 border border-neutral-800 rounded-lg overflow-hidden">
      <table class="w-full">
        <thead>
          <tr class="border-b border-neutral-800 text-left">
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Type</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Trigger</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Status</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Started</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Duration</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Map</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-neutral-800">
          <tr v-if="wipeStore.history.length === 0">
            <td colspan="6" class="px-4 py-12 text-center text-neutral-500 text-sm">
              <template v-if="wipeStore.isLoading">Loading history...</template>
              <template v-else>No wipe history yet.</template>
            </td>
          </tr>
          <tr
            v-for="wipe in wipeStore.history"
            :key="wipe.id"
            class="hover:bg-neutral-800/50 transition-colors"
          >
            <td class="px-4 py-3 text-sm font-medium text-neutral-100 capitalize">{{ wipe.wipe_type }}</td>
            <td class="px-4 py-3 text-sm text-neutral-400 capitalize">{{ wipe.trigger_type.replace('_', ' ') }}</td>
            <td class="px-4 py-3">
              <span class="text-xs font-medium px-2 py-0.5 rounded-full" :class="statusBadgeClass(wipe.status)">
                {{ wipe.status.replace('_', ' ') }}
              </span>
            </td>
            <td class="px-4 py-3 text-sm text-neutral-400">
              {{ wipe.started_at ? new Date(wipe.started_at).toLocaleString() : '\u2014' }}
            </td>
            <td class="px-4 py-3 text-sm text-neutral-400 font-mono">
              {{ duration(wipe.started_at, wipe.completed_at) }}
            </td>
            <td class="px-4 py-3 text-sm text-neutral-400">{{ wipe.map_used || '\u2014' }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>
