<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useApi } from '@/composables/useApi'
import type { WebstoreItem } from '@/types'
import { ShoppingBag, Plus, Trash2, RefreshCw, DollarSign } from 'lucide-vue-next'

const api = useApi()

const items = ref<WebstoreItem[]>([])
const isLoading = ref(false)

function typeBadgeClass(type: string): string {
  switch (type) {
    case 'kit': return 'bg-blue-500/15 text-blue-400'
    case 'rank': return 'bg-purple-500/15 text-purple-400'
    case 'currency': return 'bg-yellow-500/15 text-yellow-400'
    case 'custom_command': return 'bg-oxide-500/15 text-oxide-400'
    default: return 'bg-neutral-700/50 text-neutral-400'
  }
}

async function fetchItems() {
  isLoading.value = true
  try {
    const data = await api.get<{ items: WebstoreItem[] }>('/store/items')
    items.value = data.items
  } catch {
    // API not wired yet
  } finally {
    isLoading.value = false
  }
}

async function deleteItem(item: WebstoreItem) {
  if (!confirm(`Delete ${item.item_name}?`)) return
  try {
    await api.del(`/store/items/${item.id}`)
    await fetchItems()
  } catch {
    // Handle error
  }
}

onMounted(() => {
  fetchItems()
})
</script>

<template>
  <div class="p-6 space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div class="flex items-center gap-3">
        <ShoppingBag class="w-5 h-5 text-oxide-500" />
        <div>
          <h1 class="text-2xl font-bold text-neutral-100">Webstore</h1>
          <p class="text-sm text-neutral-500 mt-0.5">{{ items.length }} items</p>
        </div>
      </div>
      <div class="flex items-center gap-3">
        <button
          @click="fetchItems"
          :disabled="isLoading"
          class="flex items-center gap-2 px-3 py-2 text-sm text-neutral-400 hover:text-neutral-200 bg-neutral-800 hover:bg-neutral-700 rounded-lg transition-colors"
        >
          <RefreshCw class="w-4 h-4" :class="{ 'animate-spin': isLoading }" />
        </button>
        <button class="flex items-center gap-2 px-4 py-2 bg-oxide-600 hover:bg-oxide-700 text-white text-sm font-medium rounded-lg transition-colors">
          <Plus class="w-4 h-4" />
          Add Item
        </button>
      </div>
    </div>

    <!-- Items 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">Item</th>
            <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">Price</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">Commands</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider text-right">Actions</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-neutral-800">
          <tr v-if="items.length === 0">
            <td colspan="6" class="px-4 py-12 text-center text-neutral-500 text-sm">
              <template v-if="isLoading">Loading store items...</template>
              <template v-else>No store items yet. Add items to start selling.</template>
            </td>
          </tr>
          <tr
            v-for="item in items"
            :key="item.id"
            class="hover:bg-neutral-800/50 transition-colors"
          >
            <td class="px-4 py-3">
              <p class="text-sm font-medium text-neutral-100">{{ item.item_name }}</p>
              <p v-if="item.description" class="text-xs text-neutral-500 truncate max-w-xs">{{ item.description }}</p>
            </td>
            <td class="px-4 py-3">
              <span class="text-xs font-medium px-2 py-0.5 rounded-full" :class="typeBadgeClass(item.item_type)">
                {{ item.item_type.replace('_', ' ') }}
              </span>
            </td>
            <td class="px-4 py-3">
              <div class="flex items-center gap-1 text-sm text-neutral-200">
                <DollarSign class="w-3.5 h-3.5 text-neutral-500" />
                {{ item.price.toFixed(2) }}
              </div>
            </td>
            <td class="px-4 py-3">
              <span
                class="text-xs font-medium px-2 py-0.5 rounded-full"
                :class="item.is_active ? 'bg-green-500/10 text-green-400' : 'bg-neutral-700/50 text-neutral-400'"
              >
                {{ item.is_active ? 'Active' : 'Inactive' }}
              </span>
            </td>
            <td class="px-4 py-3 text-sm text-neutral-400 font-mono">
              {{ item.delivery_config.commands.length }} cmd{{ item.delivery_config.commands.length !== 1 ? 's' : '' }}
            </td>
            <td class="px-4 py-3 text-right">
              <button
                @click="deleteItem(item)"
                class="p-1.5 text-neutral-500 hover:text-red-400 rounded transition-colors"
                title="Delete item"
              >
                <Trash2 class="w-4 h-4" />
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>
