<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { usePluginStore } from '@/stores/plugins'
import type { PluginEntry } from '@/types'
import { Puzzle, Search, Download, RefreshCw, Power, PowerOff, Trash2 } from 'lucide-vue-next'

const pluginStore = usePluginStore()

const searchQuery = ref('')
const tab = ref<'installed' | 'browse'>('installed')

const filteredPlugins = computed(() => {
  let result = pluginStore.plugins
  if (searchQuery.value.trim()) {
    const q = searchQuery.value.toLowerCase()
    result = result.filter((p: PluginEntry) => p.plugin_name.toLowerCase().includes(q))
  }
  return result
})

const loadedCount = computed(() => pluginStore.plugins.filter((p: PluginEntry) => p.is_loaded).length)

function sourceLabel(source: string): string {
  switch (source) {
    case 'umod': return 'uMod'
    case 'corrosion_module': return 'Corrosion'
    case 'manual': return 'Manual'
    default: return source
  }
}

function sourceBadgeClass(source: string): string {
  switch (source) {
    case 'umod': return 'bg-green-500/10 text-green-400'
    case 'corrosion_module': return 'bg-oxide-500/15 text-oxide-400'
    default: return 'bg-neutral-700/50 text-neutral-400'
  }
}

onMounted(() => {
  pluginStore.fetchPlugins()
})
</script>

<template>
  <div class="p-6 space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div class="flex items-center gap-3">
        <Puzzle class="w-5 h-5 text-oxide-500" />
        <div>
          <h1 class="text-2xl font-bold text-neutral-100">Plugins</h1>
          <p class="text-sm text-neutral-500 mt-0.5">
            {{ loadedCount }} loaded / {{ pluginStore.plugins.length }} installed
          </p>
        </div>
      </div>
      <button
        @click="pluginStore.fetchPlugins()"
        :disabled="pluginStore.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': pluginStore.isLoading }" />
        Refresh
      </button>
    </div>

    <!-- Tabs + Search -->
    <div class="flex items-center gap-4">
      <div class="flex bg-neutral-800 rounded-lg border border-neutral-700 overflow-hidden">
        <button
          @click="tab = 'installed'"
          class="px-4 py-2 text-sm font-medium transition-colors"
          :class="tab === 'installed' ? 'bg-oxide-500/15 text-oxide-400' : 'text-neutral-400 hover:text-neutral-200'"
        >
          Installed
        </button>
        <button
          @click="tab = 'browse'"
          class="px-4 py-2 text-sm font-medium transition-colors"
          :class="tab === 'browse' ? 'bg-oxide-500/15 text-oxide-400' : 'text-neutral-400 hover:text-neutral-200'"
        >
          Browse uMod
        </button>
      </div>
      <div class="relative flex-1 max-w-sm">
        <Search class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-neutral-500" />
        <input
          v-model="searchQuery"
          type="text"
          :placeholder="tab === 'installed' ? 'Search installed plugins...' : 'Search uMod...'"
          class="w-full pl-10 pr-3 py-2 bg-neutral-800 border border-neutral-700 rounded-lg text-sm text-neutral-100 placeholder-neutral-500 focus:outline-none focus:ring-2 focus:ring-oxide-500/50 focus:border-oxide-500 transition-colors"
        />
      </div>
    </div>

    <!-- Installed Plugins -->
    <div v-if="tab === 'installed'" 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">Plugin</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Version</th>
            <th class="px-4 py-3 text-xs font-medium text-neutral-500 uppercase tracking-wider">Source</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">Wipe Behavior</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="filteredPlugins.length === 0">
            <td colspan="6" class="px-4 py-12 text-center text-neutral-500 text-sm">
              <template v-if="pluginStore.isLoading">Loading plugins...</template>
              <template v-else>No plugins installed yet.</template>
            </td>
          </tr>
          <tr
            v-for="plugin in filteredPlugins"
            :key="plugin.id"
            class="hover:bg-neutral-800/50 transition-colors"
          >
            <td class="px-4 py-3 text-sm font-medium text-neutral-100">{{ plugin.plugin_name }}</td>
            <td class="px-4 py-3 text-sm text-neutral-400 font-mono">{{ plugin.plugin_version || '\u2014' }}</td>
            <td class="px-4 py-3">
              <span class="text-xs font-medium px-2 py-0.5 rounded-full" :class="sourceBadgeClass(plugin.source)">
                {{ sourceLabel(plugin.source) }}
              </span>
            </td>
            <td class="px-4 py-3">
              <span
                class="inline-flex items-center gap-1.5 text-xs font-medium"
                :class="plugin.is_loaded ? 'text-green-400' : 'text-neutral-500'"
              >
                <span class="h-1.5 w-1.5 rounded-full" :class="plugin.is_loaded ? 'bg-green-500' : 'bg-neutral-600'" />
                {{ plugin.is_loaded ? 'Loaded' : 'Unloaded' }}
              </span>
            </td>
            <td class="px-4 py-3 text-xs text-neutral-500">
              <template v-if="plugin.never_wipe">Never wipe</template>
              <template v-else>
                {{ [plugin.wipe_on_map && 'Map', plugin.wipe_on_bp && 'BP', plugin.wipe_on_full && 'Full'].filter(Boolean).join(', ') || 'None' }}
              </template>
            </td>
            <td class="px-4 py-3 text-right">
              <div class="flex items-center justify-end gap-1">
                <button
                  class="p-1.5 rounded transition-colors"
                  :class="plugin.is_loaded ? 'text-neutral-500 hover:text-yellow-400' : 'text-neutral-500 hover:text-green-400'"
                  :title="plugin.is_loaded ? 'Unload' : 'Load'"
                >
                  <component :is="plugin.is_loaded ? PowerOff : Power" class="w-4 h-4" />
                </button>
                <button class="p-1.5 text-neutral-500 hover:text-red-400 rounded transition-colors" title="Uninstall">
                  <Trash2 class="w-4 h-4" />
                </button>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- Browse uMod (placeholder) -->
    <div v-if="tab === 'browse'" class="bg-neutral-900 border border-neutral-800 rounded-lg p-12 text-center">
      <Download class="w-10 h-10 text-neutral-600 mx-auto mb-3" />
      <h3 class="text-lg font-medium text-neutral-300 mb-1">uMod Plugin Browser</h3>
      <p class="text-sm text-neutral-500">Search and install plugins directly from uMod. Coming soon.</p>
    </div>
  </div>
</template>
