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 { CardType } from '@/pages/admin/card-types/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: {
  onEdit: (row: CardType) => void
  onToggleStatus: (row: CardType) => void
  onDelete: (row: CardType) => void
}, t: Composer['t']): ColumnDef<CardType>[] {
  return [
    {
      accessorKey: 'name',
      header: () => t('admin.cardTypes.columns.name'),
      cell: ({ row }) => {
        return row.getValue('name') as string
      },
    },
    {
      accessorKey: 'description',
      header: () => t('admin.cardTypes.columns.description'),
      cell: ({ row }) => {
        const description = row.getValue('description') as string
        return description || '-'
      },
    },
    {
      accessorKey: 'application.name',
      header: () => t('admin.cardTypes.columns.application'),
      cell: ({ row }) => {
        const appName = row.original.application?.name
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'recharge_type',
      header: () => '充值类型',
      cell: ({ row }) => {
        const rechargeType = row.original.recharge_type
        const isBalance = rechargeType === 'balance'
        return h(Badge, { variant: isBalance ? 'outline' : 'default' }, () => isBalance ? '余额充值' : '订阅充值')
      },
    },
    {
      accessorKey: 'price',
      header: () => t('admin.cardTypes.columns.price'),
      cell: ({ row }) => {
        const price = row.getValue('price') as number
        return `¥${price}`
      },
    },
    {
      accessorKey: 'value',
      header: () => t('admin.cardTypes.columns.value'),
      cell: ({ row }) => {
        const value = row.getValue('value') as number
        const rechargeType = row.original.recharge_type
        const valueUnit = row.original.value_unit

        if (value === -1) {
          return h(Badge, { variant: 'default' }, () => t('admin.cardTypes.units.permanent'))
        }

        if (rechargeType === 'subscription' && valueUnit) {
          const unitMap: Record<string, string> = {
            minute: '分钟',
            hour: '小时',
            day: '天',
            month: '月',
            year: '年',
          }
          return `${value} ${unitMap[valueUnit] || valueUnit}`
        }

        return `${value}`
      },
    },
    {
      accessorKey: 'generatedCount',
      header: () => t('admin.cardTypes.columns.generatedCount'),
      cell: ({ row }) => {
        const count = row.original.generatedCount ?? 0
        return `${count} ${t('admin.cardTypes.units.cards')}`
      },
    },
    {
      accessorKey: 'status',
      header: () => t('admin.cardTypes.columns.status'),
      cell: ({ row }) => {
        const status = row.getValue('status') as string
        const isActive = status === 'active'
        return h(Badge, { variant: isActive ? 'default' : 'destructive' }, () => t(`admin.cardTypes.statusOptions.${status}`))
      },
    },
    {
      accessorKey: 'created_at',
      header: () => t('admin.cardTypes.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 '-'
        }
      },
    },
    {
      id: 'actions',
      header: () => h('span', { class: 'sr-only' }, t('common.actions')),
      cell: ({ row }) => {
        const cardType = row.original
        const isActive = cardType.status === 'active'

        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(cardType) }, () => [
                    h(Pencil, { class: 'mr-2 h-4 w-4' }),
                    t('admin.cardTypes.edit'),
                  ]),
                  h(DropdownMenuItem, { onClick: () => actions.onToggleStatus(cardType) }, () => [
                    h(Ban, { class: 'mr-2 h-4 w-4' }),
                    isActive ? t('admin.cardTypes.disable') : t('admin.cardTypes.enable'),
                  ]),
                  h(DropdownMenuSeparator),
                  h(DropdownMenuItem, { class: 'text-destructive', onClick: () => actions.onDelete(cardType) }, () => [
                    h(Trash2, { class: 'mr-2 h-4 w-4' }),
                    t('admin.cardTypes.delete'),
                  ]),
                ],
              ),
            ],
          },
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]
}
