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

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

const emit = defineEmits<{
  refresh: []
  batchAction: [action: 'enable' | 'disable' | 'delete', rows: CloudConstant[]]
}>()

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  select: 'admin.cloudConstants.select',
  key: 'admin.cloudConstants.columns.key',
  value: 'admin.cloudConstants.columns.value',
  var_type: 'admin.cloudConstants.columns.type',
  description: 'admin.cloudConstants.columns.description',
  application_name: 'admin.cloudConstants.columns.application',
  status: 'admin.cloudConstants.columns.status',
  created_at: 'admin.cloudConstants.columns.createdAt',
}

defineExpose({
  table,
})

function handleBatchAction(action: 'enable' | 'disable' | 'delete') {
  const selectedRows = table.getSelectedRowModel().rows.map(row => row.original)
  emit('batchAction', action, selectedRows)
  table.resetRowSelection()
}
</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="constants">
          <UiButton variant="outline" size="sm" @click="handleBatchAction('enable')">
            {{ t('admin.cloudConstants.batchEnableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="handleBatchAction('disable')">
            {{ t('admin.cloudConstants.batchDisableBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="handleBatchAction('delete')">
            {{ t('admin.cloudConstants.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>
