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

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

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

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  select: 'admin.cloudVariables.select',
  key: 'admin.cloudVariables.columns.key',
  default_value: 'admin.cloudVariables.columns.defaultValue',
  scope: 'admin.cloudVariables.columns.scope',
  write_permission: 'admin.cloudVariables.create.writePermission',
  var_type: 'admin.cloudVariables.columns.type',
  description: 'admin.cloudVariables.columns.description',
  application_name: 'admin.cloudVariables.columns.application',
  status: 'admin.cloudVariables.columns.status',
  created_at: 'admin.cloudVariables.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="variables">
          <UiButton variant="outline" size="sm" @click="handleBatchAction('enable')">
            {{ t('admin.cloudVariables.batchEnableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="handleBatchAction('disable')">
            {{ t('admin.cloudVariables.batchDisableBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="handleBatchAction('delete')">
            {{ t('admin.cloudVariables.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>
