// @iconify/vue 保留用于品牌图标（支付宝、微信、Stripe、PayPal、USDT），这些图标在 lucide 中不存在
import { Icon } from '@iconify/vue'
import { CreditCard, Edit, Trash2, Wallet } from 'lucide-vue-next'
import type { ColumnDef } from '@tanstack/vue-table'

import { h } from 'vue'

import type { PaymentChannel } from '@/pages/admin/payment-channels/data/schema'

import Badge from '@/components/ui/badge/Badge.vue'
import Button from '@/components/ui/button/Button.vue'

interface ColumnOptions {
  onToggleStatus: (row: PaymentChannel) => void
  onEdit: (row: PaymentChannel) => void
  onDelete: (row: PaymentChannel) => void
}

export function getColumns(options: ColumnOptions, t: (key: string) => string): ColumnDef<PaymentChannel>[] {
  const typeLabels: Record<string, string> = {
    alipay: '支付宝',
    wechat: '微信支付',
    stripe: 'Stripe',
    paypal: 'PayPal',
    bepusdt: 'BEPUSDT',
    epay: '易支付',
    other: '其他',
  }

  const typeIcons: Record<string, any> = {
    alipay: 'ri:alipay-fill',
    wechat: 'ri:wechat-pay-fill',
    stripe: 'logos:stripe',
    paypal: 'logos:paypal',
    bepusdt: 'cryptocurrency:usdt',
    epay: Wallet,
    other: CreditCard,
  }

  const brandIcons = new Set(['ri:alipay-fill', 'ri:wechat-pay-fill', 'logos:stripe', 'logos:paypal', 'cryptocurrency:usdt'])

  return [
    {
      accessorKey: 'name',
      header: () => '渠道名称',
      cell: ({ row }) => {
        return h('div', { class: 'flex items-center gap-2' }, [
          row.original.icon
            ? h('img', { src: row.original.icon, class: 'h-5 w-5 rounded', alt: '' })
            : (() => {
                const icon = typeIcons[row.original.type] || CreditCard
                return typeof icon === 'string' && brandIcons.has(icon)
                  ? h(Icon, { icon, class: 'h-5 w-5' })
                  : h(icon, { class: 'h-5 w-5' })
              })(),
          h('span', { class: 'font-medium' }, row.original.name),
        ])
      },
    },
    {
      accessorKey: 'type',
      header: () => '渠道类型',
      cell: ({ row }) => {
        return h(Badge, { variant: 'secondary' }, () => typeLabels[row.original.type] || row.original.type)
      },
    },
    {
      accessorKey: 'sort',
      header: () => '排序',
      cell: ({ row }) => {
        return h('span', { class: 'text-muted-foreground' }, row.original.sort)
      },
    },
    {
      accessorKey: 'status',
      header: () => '状态',
      cell: ({ row }) => {
        const statusLabels: Record<string, string> = {
          active: '启用',
          inactive: '禁用',
        }
        const statusClasses: Record<string, string> = {
          active: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400 cursor-pointer',
          inactive: 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400 cursor-pointer',
        }
        return h(Badge, {
          class: statusClasses[row.original.status],
          onClick: () => options.onToggleStatus(row.original),
        }, () => statusLabels[row.original.status])
      },
    },
    {
      accessorKey: 'remark',
      header: () => '备注',
      cell: ({ row }) => {
        return h('span', { class: 'text-sm text-muted-foreground truncate max-w-[200px] block' }, row.original.remark || '-')
      },
    },
    {
      accessorKey: 'created_at',
      header: () => '创建时间',
      cell: ({ row }) => {
        return h('span', { class: 'text-sm text-muted-foreground' }, new Date(row.original.created_at).toLocaleString('zh-CN'))
      },
    },
    {
      id: 'actions',
      header: () => '操作',
      cell: ({ row }) => {
        return h('div', { class: 'flex items-center justify-end gap-1' }, [
          h(Button, {
            variant: 'ghost',
            size: 'sm',
            onClick: () => options.onEdit(row.original),
          }, () => [
            h(Edit, { class: 'h-4 w-4' }),
          ]),
          h(Button, {
            variant: 'ghost',
            size: 'sm',
            onClick: () => options.onDelete(row.original),
          }, () => [
            h(Trash2, { class: 'h-4 w-4 text-destructive' }),
          ]),
        ])
      },
    },
  ]
}
