<script setup lang="ts">
import type { ColumnDef } from '@tanstack/vue-table'

import { computed } from 'vue'
import { useI18n } from 'vue-i18n'

import type { DataTableProps } from '@/components/data-table/types'
import type { Webhook } from '@/pages/admin/extension/data/schema'

import BulkActions from '@/components/data-table/bulk-actions.vue'
import DataTable from '@/components/data-table/data-table.vue'
import { SelectColumn } from '@/components/data-table/table-columns'
import { generateVueTable } from '@/components/data-table/use-generate-vue-table'
import DataTableViewOptions from '@/components/data-table/view-options.vue'
import { getWebhookColumns } from '@/pages/admin/extension/components/webhook-columns'
import WebhookDataTableToolbar from '@/pages/admin/extension/components/webhook-data-table-toolbar.vue'

const props = defineProps<Omit<DataTableProps<Webhook>, 'columns'> & {
  onToggleStatus: (row: Webhook) => void
  onTest: (row: Webhook) => void
  onDelete: (row: Webhook) => void
  searchFilter?: string
}>()

const emit = defineEmits<{
  'refresh': []
  'batchEnable': []
  'batchDisable': []
  'batchDelete': []
  'update:searchFilter': [value: string]
}>()

const { t } = useI18n()

const columns = computed(() => [
  SelectColumn as ColumnDef<Webhook>,
  ...getWebhookColumns({
    onToggleStatus: props.onToggleStatus,
    onTest: props.onTest,
    onDelete: props.onDelete,
  }, t),
])

const table = generateVueTable<Webhook>({
  get data() { return props.data },
  get loading() { return props.loading },
  columns: columns.value,
}, columns.value)

const columnLabels: Record<string, string> = {
  select: 'admin.extension.webhooks.select',
  name: 'admin.extension.webhooks.columns.name',
  url: 'admin.extension.webhooks.columns.url',
  application_name: 'admin.extension.webhooks.columns.application',
  events: 'admin.extension.webhooks.columns.events',
  status: 'admin.extension.webhooks.columns.status',
  retry_count: 'admin.extension.webhooks.columns.retryCount',
  timeout: 'admin.extension.webhooks.columns.timeout',
  created_at: 'admin.extension.webhooks.columns.createdAt',
}

defineExpose({
  table,
})
</script>

<template>
  <DataTable :columns="columns" :data :loading :table @refresh="emit('refresh')">
    <template #toolbar>
      <div class="space-y-3">
        <BulkActions :table="table" entity-name="webhooks">
          <UiButton variant="outline" size="sm" @click="emit('batchEnable')">
            {{ t('admin.extension.webhooks.enable') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchDisable')">
            {{ t('admin.extension.webhooks.disable') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.extension.webhooks.batchDelete') }}
          </UiButton>
        </BulkActions>
        <div class="flex flex-wrap items-center justify-between gap-2">
          <div class="flex flex-wrap items-center gap-2">
            <WebhookDataTableToolbar
              :table
              :search-filter="searchFilter"
              @update:search-filter="emit('update:searchFilter', $event)"
            />
            <slot name="filters" />
          </div>
          <div class="flex items-center gap-2">
            <DataTableViewOptions :table="table" :column-labels="columnLabels" />
          </div>
        </div>
      </div>
    </template>
  </DataTable>
</template>
