<script setup lang="ts">
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { Package, Check, Lock } from 'lucide-vue-next'

const auth = useAuthStore()

interface Module {
  slug: string
  name: string
  description: string
  price: string
  features: string[]
}

const modules: Module[] = [
  {
    slug: 'analytics',
    name: 'Analytics & Insights',
    description: 'Player trends, retention metrics, and server performance dashboards.',
    price: '$4.99/mo',
    features: ['Player count history', 'Performance graphs', 'Retention reports', 'Export to CSV'],
  },
  {
    slug: 'webstore',
    name: 'Webstore',
    description: 'Sell kits, ranks, and custom items with Stripe/PayPal checkout.',
    price: '$9.99/mo',
    features: ['Item catalog', 'Stripe + PayPal', 'Auto-delivery via RCON', 'Transaction history'],
  },
  {
    slug: 'discord_bot',
    name: 'Discord Bot',
    description: 'Two-way Discord integration with chat relay and command bridge.',
    price: '$2.99/mo',
    features: ['Chat relay', 'Server status embed', 'Player lookup', 'Admin commands'],
  },
  {
    slug: 'backups',
    name: 'Cloud Backups',
    description: 'Automated server backups with point-in-time restore.',
    price: '$5.99/mo',
    features: ['Scheduled backups', 'Manual snapshots', 'One-click restore', '30-day retention'],
  },
]

const tab = ref<'available' | 'installed'>('available')

function isEnabled(slug: string): boolean {
  return auth.license?.modules_enabled?.includes(slug) ?? false
}
</script>

<template>
  <div class="p-6 space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div class="flex items-center gap-3">
        <Package class="w-5 h-5 text-oxide-500" />
        <h1 class="text-2xl font-bold text-neutral-100">Module Store</h1>
      </div>
      <div class="flex bg-neutral-800 rounded-lg border border-neutral-700 overflow-hidden">
        <button
          @click="tab = 'available'"
          class="px-4 py-2 text-sm font-medium transition-colors"
          :class="tab === 'available' ? 'bg-oxide-500/15 text-oxide-400' : 'text-neutral-400 hover:text-neutral-200'"
        >
          Available
        </button>
        <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>
      </div>
    </div>

    <!-- Module cards -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
      <div
        v-for="mod in (tab === 'installed' ? modules.filter(m => isEnabled(m.slug)) : modules)"
        :key="mod.slug"
        class="bg-neutral-900 border rounded-lg p-5 transition-colors"
        :class="isEnabled(mod.slug) ? 'border-oxide-500/30' : 'border-neutral-800'"
      >
        <div class="flex items-start justify-between mb-3">
          <div>
            <h3 class="text-base font-semibold text-neutral-100">{{ mod.name }}</h3>
            <p class="text-sm text-neutral-500 mt-0.5">{{ mod.description }}</p>
          </div>
          <span class="text-sm font-medium text-oxide-400 shrink-0 ml-4">{{ mod.price }}</span>
        </div>

        <ul class="space-y-1.5 mb-4">
          <li v-for="feat in mod.features" :key="feat" class="flex items-center gap-2 text-sm text-neutral-400">
            <Check class="w-3.5 h-3.5 text-oxide-500 shrink-0" />
            {{ feat }}
          </li>
        </ul>

        <button
          v-if="isEnabled(mod.slug)"
          disabled
          class="w-full py-2 text-sm font-medium text-green-400 bg-green-500/10 border border-green-500/20 rounded-lg cursor-default"
        >
          Installed
        </button>
        <button
          v-else
          class="w-full flex items-center justify-center gap-2 py-2 text-sm font-medium text-oxide-400 bg-oxide-500/10 hover:bg-oxide-500/20 border border-oxide-500/20 rounded-lg transition-colors"
        >
          <Lock class="w-3.5 h-3.5" />
          Subscribe
        </button>
      </div>
    </div>

    <div v-if="tab === 'installed' && modules.filter(m => isEnabled(m.slug)).length === 0" class="bg-neutral-900 border border-neutral-800 rounded-lg p-12 text-center">
      <Package class="w-10 h-10 text-neutral-600 mx-auto mb-3" />
      <h3 class="text-lg font-medium text-neutral-300 mb-1">No Modules Installed</h3>
      <p class="text-sm text-neutral-500">Browse available modules to extend your panel.</p>
    </div>
  </div>
</template>
