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

import { useI18n } from 'vue-i18n'

import type { DataTableProps } from '@/components/data-table/types'
import type { DynamicCode } from '@/pages/admin/dynamic-code/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/dynamic-code/components/columns'
import DataTableToolbar from '@/pages/admin/dynamic-code/components/data-table-toolbar.vue'

const props = defineProps<Omit<DataTableProps<DynamicCode>, 'columns'> & {
  onToggleStatus: (row: DynamicCode) => void
  onEdit: (row: DynamicCode) => void
  onDelete: (row: DynamicCode) => void
}>()

const emit = defineEmits<{
  refresh: []
  batchEnable: []
  batchDisable: []
  batchDelete: []
}>()

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  select: 'admin.cloudFunction.select',
  name: 'admin.cloudFunction.columns.name',
  application_name: 'admin.cloudFunction.columns.application',
  description: 'admin.cloudFunction.columns.description',
  enabled: 'admin.cloudFunction.columns.status',
  creator_username: 'admin.cloudFunction.columns.creator',
  created_at: 'admin.cloudFunction.columns.createdAt',
  updated_at: 'admin.cloudFunction.columns.updatedAt',
}

defineExpose({
  table,
})
</script>

<template>
  <DataTable :columns="columns" :data :loading :table @refresh="emit('refresh')">
    <template #toolbar>
      <div class="space-y-3">
        <div class="flex items-center justify-end">
          <slot name="actions" />
        </div>
        <BulkActions :table="table" entity-name="functions">
          <UiButton variant="outline" size="sm" @click="emit('batchEnable')">
            {{ t('admin.cloudFunction.batchEnableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchDisable')">
            {{ t('admin.cloudFunction.batchDisableBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.cloudFunction.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 />
            <slot name="filters" />
          </div>
          <DataTableViewOptions :table="table" :column-labels="columnLabels" />
        </div>
      </div>
    </template>
  </DataTable>
</template>
