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

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

const emit = defineEmits<{
  refresh: []
  batchUnban: []
  batchBan: []
  batchDelete: []
}>()

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  'select': 'admin.users.select',
  'username': 'admin.users.columns.username',
  'email': 'admin.users.columns.email',
  'application.name': 'admin.users.columns.application',
  'online_status': 'admin.users.columns.status',
  'balance': 'admin.users.columns.balance',
  'expiry_at': 'admin.users.columns.expiryAt',
  'last_login_at': 'admin.users.columns.lastLoginAt',
  'created_at': 'admin.users.columns.createdAt',
}

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="users">
          <UiButton variant="outline" size="sm" @click="emit('batchUnban')">
            {{ t('admin.users.batchUnbanBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchBan')">
            {{ t('admin.users.batchBanBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.users.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>
