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

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

import type { AgentApp } from '@/pages/admin/agent-apps/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: {
  onView: (row: AgentApp) => void
  onEdit: (row: AgentApp) => void
  onDelete: (row: AgentApp) => void
}): ColumnDef<AgentApp>[] {
  return [
    {
      accessorKey: 'agent_name',
      header: '代理',
      cell: ({ row }) => {
        const name = row.getValue('agent_name') as string
        const email = row.original.agent_email
        return h('div', { class: 'flex items-center space-x-3' }, [
          h('div', { class: 'h-8 w-8 rounded bg-primary/10 flex items-center justify-center flex-shrink-0' }, [
            h('span', { class: 'text-primary font-bold text-sm' }, name ? name.charAt(0).toUpperCase() : '?'),
          ]),
          h('div', {}, [
            h('p', { class: 'font-medium' }, name || '-'),
            email ? h('p', { class: 'text-xs text-muted-foreground' }, email) : null,
          ]),
        ])
      },
    },
    {
      accessorKey: 'app_name',
      header: '应用',
      cell: ({ row }) => {
        const appName = row.getValue('app_name') as string
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'card_types',
      header: '卡密权限',
      cell: ({ row }) => {
        const cardTypes = row.original.card_types || []
        if (cardTypes.length === 0)
          return h('span', { class: 'text-muted-foreground text-sm' }, '-')
        const authorized = cardTypes.filter(ct => ct.can_generate).length
        return h('span', { class: 'text-sm' }, `${authorized}/${cardTypes.length}`)
      },
    },
    {
      accessorKey: 'balance',
      header: '余额',
      cell: ({ row }) => {
        const balance = row.getValue('balance') as number
        return h('span', { class: 'font-medium' }, `¥${(balance || 0).toFixed(2)}`)
      },
    },
    {
      accessorKey: 'status',
      header: '状态',
      cell: ({ row }) => {
        const status = row.getValue('status') as string
        const isActive = status === 'active'
        return h(Badge, { variant: isActive ? 'default' : 'secondary' }, () => isActive ? '已启用' : '已禁用')
      },
    },
    {
      accessorKey: 'created_at',
      header: '授权时间',
      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' }, '操作'),
      cell: ({ row }) => {
        const item = row.original
        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' }, '打开菜单'),
                ]),
              ),
              h(
                DropdownMenuContent,
                { align: 'end' },
                () => [
                  h(DropdownMenuItem, { onClick: () => actions.onView(item) }, () => [
                    h(Eye, { class: 'mr-2 h-4 w-4' }),
                    '查看授权',
                  ]),
                  h(DropdownMenuItem, { onClick: () => actions.onEdit(item) }, () => [
                    h(Pencil, { class: 'mr-2 h-4 w-4' }),
                    '编辑授权',
                  ]),
                  h(DropdownMenuSeparator),
                  h(DropdownMenuItem, { class: 'text-destructive', onClick: () => actions.onDelete(item) }, () => [
                    h(Trash2, { class: 'mr-2 h-4 w-4' }),
                    '移除授权',
                  ]),
                ],
              ),
            ],
          },
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]
}
