<script setup lang="ts">
const documents = useDocumentsStore()

const filterType = ref<string | undefined>(undefined)
const filterStatus = ref<string | undefined>(undefined)

const docTypeOptions = [
  { label: 'Tous les types', value: undefined },
  { label: 'Licence', value: 'licence' },
  { label: 'Engagement', value: 'engagement' },
  { label: 'Reglement', value: 'reglement' },
  { label: 'Constitution', value: 'constitution' },
]

const statusOptions = [
  { label: 'Tous les statuts', value: undefined },
  { label: 'Brouillon', value: 'draft' },
  { label: 'Actif', value: 'active' },
  { label: 'Archive', value: 'archived' },
]

const columns = [
  { key: 'title', label: 'Titre' },
  { key: 'doc_type', label: 'Type' },
  { key: 'version', label: 'Version' },
  { key: 'status', label: 'Statut' },
  { key: 'items_count', label: 'Items' },
  { key: 'updated_at', label: 'Mis a jour' },
]

async function loadDocuments() {
  await documents.fetchAll({
    doc_type: filterType.value,
    status: filterStatus.value,
  })
}

onMounted(() => {
  loadDocuments()
})

watch([filterType, filterStatus], () => {
  loadDocuments()
})

const statusColor = (status: string) => {
  switch (status) {
    case 'active': return 'success'
    case 'draft': return 'warning'
    case 'archived': return 'neutral'
    default: return 'neutral'
  }
}

const statusLabel = (status: string) => {
  switch (status) {
    case 'active': return 'Actif'
    case 'draft': return 'Brouillon'
    case 'archived': return 'Archive'
    default: return status
  }
}

const typeLabel = (docType: string) => {
  switch (docType) {
    case 'licence': return 'Licence'
    case 'engagement': return 'Engagement'
    case 'reglement': return 'Reglement'
    case 'constitution': return 'Constitution'
    default: return docType
  }
}

function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString('fr-FR', {
    day: 'numeric',
    month: 'short',
    year: 'numeric',
  })
}
</script>

<template>
  <div class="space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div>
        <h1 class="text-2xl font-bold text-gray-900 dark:text-white">
          Documents de reference
        </h1>
        <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
          Documents fondateurs de la communaute Duniter/G1 sous vote permanent
        </p>
      </div>
    </div>

    <!-- Filters -->
    <div class="flex flex-wrap gap-4">
      <USelect
        v-model="filterType"
        :items="docTypeOptions"
        placeholder="Type de document"
        class="w-48"
      />
      <USelect
        v-model="filterStatus"
        :items="statusOptions"
        placeholder="Statut"
        class="w-48"
      />
    </div>

    <!-- Loading state -->
    <template v-if="documents.loading">
      <div class="space-y-3">
        <USkeleton v-for="i in 5" :key="i" class="h-12 w-full" />
      </div>
    </template>

    <!-- Error state -->
    <template v-else-if="documents.error">
      <UCard>
        <div class="flex items-center gap-3 text-red-500">
          <UIcon name="i-lucide-alert-circle" class="text-xl" />
          <p>{{ documents.error }}</p>
        </div>
      </UCard>
    </template>

    <!-- Empty state -->
    <template v-else-if="documents.list.length === 0">
      <UCard>
        <div class="text-center py-8">
          <UIcon name="i-lucide-book-open" class="text-4xl text-gray-400 mb-3" />
          <p class="text-gray-500">Aucun document de reference pour le moment</p>
        </div>
      </UCard>
    </template>

    <!-- Documents table -->
    <template v-else>
      <UCard>
        <div class="overflow-x-auto">
          <table class="w-full text-sm">
            <thead>
              <tr class="border-b border-gray-200 dark:border-gray-700">
                <th v-for="col in columns" :key="col.key" class="text-left px-4 py-3 font-medium text-gray-500 dark:text-gray-400">
                  {{ col.label }}
                </th>
              </tr>
            </thead>
            <tbody>
              <tr
                v-for="doc in documents.list"
                :key="doc.id"
                class="border-b border-gray-100 dark:border-gray-800 hover:bg-gray-50 dark:hover:bg-gray-800/50 cursor-pointer"
                @click="navigateTo(`/documents/${doc.slug}`)"
              >
                <td class="px-4 py-3">
                  <div class="flex items-center gap-2">
                    <UIcon name="i-lucide-file-text" class="text-gray-400" />
                    <span class="font-medium text-gray-900 dark:text-white">{{ doc.title }}</span>
                  </div>
                </td>
                <td class="px-4 py-3">
                  <UBadge variant="subtle" color="primary">
                    {{ typeLabel(doc.doc_type) }}
                  </UBadge>
                </td>
                <td class="px-4 py-3 text-gray-600 dark:text-gray-400 font-mono text-xs">
                  v{{ doc.version }}
                </td>
                <td class="px-4 py-3">
                  <UBadge :color="statusColor(doc.status)" variant="subtle">
                    {{ statusLabel(doc.status) }}
                  </UBadge>
                </td>
                <td class="px-4 py-3 text-gray-600 dark:text-gray-400">
                  {{ doc.items_count }}
                </td>
                <td class="px-4 py-3 text-gray-500 text-xs">
                  {{ formatDate(doc.updated_at) }}
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </UCard>
    </template>
  </div>
</template>
