<script lang="ts" setup>
import { Link, router } from '@inertiajs/vue3'
import AdminLayout from '@/Layouts/AdminLayout.vue'
import type { KnowledgeBaseCategory } from '@/types'

interface Props {
  categories: KnowledgeBaseCategory[]
}

defineOptions({ layout: AdminLayout })

const props = defineProps<Props>()

function getCategoryDepth(category: KnowledgeBaseCategory): number {
  return category.parent_id ? 1 : 0
}

function deleteCategory(category: KnowledgeBaseCategory): void {
  if (confirm(`Are you sure you want to delete "${category.name}"? This action cannot be undone.`)) {
    router.delete(`/knowledge-base/categories/${category.id}`, {
      preserveScroll: true,
    })
  }
}

const tableHeaders = [
  { title: 'Name', key: 'name', sortable: true },
  { title: 'Slug', key: 'slug', sortable: true },
  { title: 'Parent', key: 'parent', sortable: false },
  { title: 'Articles', key: 'articles_count', sortable: true, align: 'center' as const },
  { title: 'Order', key: 'sort_order', sortable: true, align: 'center' as const },
  { title: 'Visible', key: 'is_visible', sortable: true, align: 'center' as const },
  { title: 'Actions', key: 'actions', sortable: false, align: 'center' as const },
]
</script>

<template>
  <div>
    <!-- Header -->
    <div class="d-flex align-center justify-space-between mb-6">
      <div>
        <div class="text-h4 font-weight-bold">
          KB Categories
        </div>
        <div class="text-body-2 text-medium-emphasis">
          Manage knowledge base categories
        </div>
      </div>
      <Link href="/knowledge-base/categories/create">
        <VBtn color="primary" prepend-icon="tabler-plus">
          Create Category
        </VBtn>
      </Link>
    </div>

    <!-- Categories Table -->
    <VCard>
      <VDataTable
        :headers="tableHeaders"
        :items="categories"
        :items-per-page="50"
        hover
        class="text-no-wrap"
      >
        <!-- Name -->
        <template #item.name="{ item }">
          <div :class="{ 'ms-8': getCategoryDepth(item) > 0 }">
            <VIcon
              v-if="item.icon"
              :icon="item.icon"
              size="18"
              class="me-2 text-medium-emphasis"
            />
            <span class="font-weight-medium">{{ item.name }}</span>
          </div>
        </template>

        <!-- Slug -->
        <template #item.slug="{ item }">
          <code class="text-body-2">{{ item.slug }}</code>
        </template>

        <!-- Parent -->
        <template #item.parent="{ item }">
          <span v-if="item.parent">{{ item.parent.name }}</span>
          <span v-else class="text-medium-emphasis">--</span>
        </template>

        <!-- Articles Count -->
        <template #item.articles_count="{ item }">
          <VChip size="small" variant="tonal" color="info">
            {{ item.articles_count ?? 0 }}
          </VChip>
        </template>

        <!-- Sort Order -->
        <template #item.sort_order="{ item }">
          {{ item.sort_order }}
        </template>

        <!-- Visible -->
        <template #item.is_visible="{ item }">
          <VChip
            :color="item.is_visible ? 'success' : 'error'"
            size="small"
          >
            {{ item.is_visible ? 'Visible' : 'Hidden' }}
          </VChip>
        </template>

        <!-- Actions -->
        <template #item.actions="{ item }">
          <VMenu>
            <template #activator="{ props: menuProps }">
              <VBtn
                icon="tabler-dots-vertical"
                variant="text"
                size="small"
                v-bind="menuProps"
              />
            </template>
            <VList density="compact">
              <Link :href="`/knowledge-base/categories/${item.id}/edit`" class="text-decoration-none">
                <VListItem prepend-icon="tabler-edit">
                  <VListItemTitle>Edit</VListItemTitle>
                </VListItem>
              </Link>
              <VListItem
                prepend-icon="tabler-trash"
                class="text-error"
                @click="deleteCategory(item)"
              >
                <VListItemTitle>Delete</VListItemTitle>
              </VListItem>
            </VList>
          </VMenu>
        </template>

        <!-- No data -->
        <template #no-data>
          <div class="text-center py-8">
            <VIcon icon="tabler-folder" size="48" color="disabled" class="mb-2" />
            <div class="text-medium-emphasis">
              No categories found. Create your first category to get started.
            </div>
          </div>
        </template>
      </VDataTable>
    </VCard>
  </div>
</template>
