<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 { Version } from '@/pages/admin/versions/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 { getColumns } from '@/pages/admin/versions/components/columns'
import DataTableToolbar from '@/pages/admin/versions/components/data-table-toolbar.vue'

const props = defineProps<Omit<DataTableProps<Version>, 'columns'> & {
  onDownload: (row: Version) => void
  onEdit: (row: Version) => void
  onDelete: (row: Version) => void
  searchFilter?: string
}>()

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

const { t } = useI18n()

const columns = computed(() => [
  SelectColumn as ColumnDef<Version>,
  ...getColumns({
    onDownload: props.onDownload,
    onEdit: props.onEdit,
    onDelete: props.onDelete,
  }, t),
])

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

const columnLabels: Record<string, string> = {
  select: 'admin.versions.select',
  version: 'admin.versions.columns.version',
  application_name: 'admin.versions.columns.application',
  description: 'admin.versions.columns.description',
  file_size: 'admin.versions.columns.fileSize',
  update_strategy: 'admin.versions.columns.strategy',
  update_method: 'admin.versions.columns.method',
  created_at: 'admin.versions.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="versions">
          <UiButton variant="outline" size="sm" @click="emit('batchExport')">
            {{ t('admin.versions.batchExportBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.versions.batchDeleteBtn') }}
          </UiButton>
        </BulkActions>
        <div class="flex flex-wrap items-center justify-between gap-2">
          <div class="flex flex-wrap items-center gap-2">
            <DataTableToolbar
              :table
              :search-filter="searchFilter"
              @update:search-filter="emit('update:searchFilter', $event)"
            />
            <slot name="filters" />
          </div>
          <div class="flex items-center gap-2">
            <UiButton variant="outline" size="sm" @click="emit('export')">
              {{ t('admin.versions.exportBtn') }}
            </UiButton>
            <DataTableViewOptions :table="table" :column-labels="columnLabels" />
          </div>
        </div>
      </div>
    </template>
  </DataTable>
</template>
