import type { ColumnDef } from '@tanstack/vue-table'
import type { Composer } from 'vue-i18n'

import { Ban, MoreHorizontal, Pencil, Trash2 } from 'lucide-vue-next'
import { h } from 'vue'

import type { DynamicCode } from '@/pages/admin/dynamic-code/data/schema'

import Badge from '@/components/ui/badge/Badge.vue'
import Button from '@/components/ui/button/Button.vue'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'

export function getColumns(actions: {
  onToggleStatus: (row: DynamicCode) => void
  onEdit: (row: DynamicCode) => void
  onDelete: (row: DynamicCode) => void
}, t: Composer['t']): ColumnDef<DynamicCode>[] {
  return [
    {
      accessorKey: 'name',
      header: () => t('admin.cloudFunction.columns.name'),
      cell: ({ row }) => {
        const name = row.getValue('name') as string
        return name || '-'
      },
    },
    {
      accessorKey: 'application_name',
      header: () => t('admin.cloudFunction.columns.application'),
      cell: ({ row }) => {
        const appName = row.getValue('application_name') as string
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'description',
      header: () => t('admin.cloudFunction.columns.description'),
      cell: ({ row }) => {
        const description = row.getValue('description') as string
        return description || '-'
      },
    },
    {
      accessorKey: 'enabled',
      header: () => t('admin.cloudFunction.columns.status'),
      cell: ({ row }) => {
        const enabled = row.getValue('enabled') as boolean
        return h(Badge, { variant: enabled ? 'default' : 'destructive' }, () => enabled ? t('admin.cloudFunction.enabled') : t('admin.cloudFunction.disabled'))
      },
    },
    {
      accessorKey: 'creator.username',
      header: () => t('admin.cloudFunction.columns.creator'),
      cell: ({ row }) => {
        const username = row.original.creator?.username
        return username || '-'
      },
    },
    {
      accessorKey: 'created_at',
      header: () => t('admin.cloudFunction.columns.createdAt'),
      cell: ({ row }) => {
        const createdAt = row.getValue('created_at')
        if (!createdAt)
          return '-'
        try {
          const date = new Date(createdAt as string)
          if (Number.isNaN(date.getTime()))
            return '-'
          return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
          })
        }
        catch {
          return '-'
        }
      },
    },
    {
      accessorKey: 'updated_at',
      header: () => t('admin.cloudFunction.columns.updatedAt'),
      cell: ({ row }) => {
        const updatedAt = row.getValue('updated_at')
        if (!updatedAt)
          return '-'
        try {
          const date = new Date(updatedAt as string)
          if (Number.isNaN(date.getTime()))
            return '-'
          return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
          })
        }
        catch {
          return '-'
        }
      },
    },
    {
      id: 'actions',
      header: () => h('span', { class: 'sr-only' }, t('common.actions')),
      cell: ({ row }) => {
        const code = row.original
        const isDisabled = !code.enabled

        return h(
          DropdownMenu,
          {},
          {
            default: () => [
              h(DropdownMenuTrigger, { asChild: true }, () =>
                h(Button, { variant: 'ghost', class: 'h-8 w-8 p-0' }, () => [
                  h(MoreHorizontal, { class: 'h-4 w-4' }),
                  h('span', { class: 'sr-only' }, t('common.openMenu')),
                ])),
              h(
                DropdownMenuContent,
                { align: 'end' },
                () => [
                  h(DropdownMenuItem, { onClick: () => actions.onEdit(code) }, () => [
                    h(Pencil, { class: 'mr-2 h-4 w-4' }),
                    t('admin.cloudFunction.edit.title'),
                  ]),
                  h(DropdownMenuItem, { onClick: () => actions.onToggleStatus(code) }, () => [
                    h(Ban, { class: 'mr-2 h-4 w-4' }),
                    isDisabled ? t('admin.cloudFunction.enable') : t('admin.cloudFunction.disable'),
                  ]),
                  h(DropdownMenuSeparator),
                  h(DropdownMenuItem, { class: 'text-destructive', onClick: () => actions.onDelete(code) }, () => [
                    h(Trash2, { class: 'mr-2 h-4 w-4' }),
                    t('admin.cloudFunction.delete'),
                  ]),
                ],
              ),
            ],
          },
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]
}
