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

import { h } from 'vue'

import type { Card } from '../data/schema'

import { Copy } from '@/components/sva-ui/copy'
import Badge from '@/components/ui/badge/Badge.vue'

export function getColumns(t: Composer['t']): ColumnDef<Card>[] {
  return [
    {
      accessorKey: 'code',
      header: () => t('agent.cards.columns.code'),
      cell: ({ row }) => {
        const code = row.getValue('code') as string
        if (!code) return '-'
        return h('div', { class: 'flex items-center space-x-2' }, [
          h('code', { class: 'text-xs bg-muted px-2 py-1 rounded font-mono' }, code),
          h(Copy, { class: 'h-4 w-4', size: 'sm', content: code }),
        ])
      },
    },
    {
      accessorKey: 'app_name',
      header: () => t('agent.cards.columns.appName'),
      cell: ({ row }) => {
        const appName = row.getValue('app_name') as string
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'card_type_name',
      header: () => t('agent.cards.columns.cardTypeName'),
      cell: ({ row }) => {
        const cardTypeName = row.getValue('card_type_name') as string
        return cardTypeName ? h(Badge, { variant: 'outline' }, () => cardTypeName) : '-'
      },
    },
    {
      accessorKey: 'status',
      header: () => t('agent.cards.columns.status'),
      cell: ({ row }) => {
        const status = row.getValue('status') as string
        const statusMap: Record<string, { label: string, variant: 'default' | 'secondary' | 'destructive' }> = {
          unused: { label: t('agent.cards.status.unused'), variant: 'default' },
          used: { label: t('agent.cards.status.used'), variant: 'secondary' },
          expired: { label: t('agent.cards.status.expired'), variant: 'destructive' },
          banned: { label: t('agent.cards.status.banned'), variant: 'destructive' },
        }
        const { label, variant } = statusMap[status] || { label: status || '-', variant: 'secondary' }
        return h(Badge, { variant }, () => label)
      },
    },
    {
      accessorKey: 'created_at',
      header: () => t('agent.cards.columns.createdAt'),
      cell: ({ row }) => {
        const createdAt = row.getValue('created_at') as string
        if (!createdAt) return '-'
        try {
          const date = new Date(createdAt)
          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: 'used_at',
      header: () => t('agent.cards.columns.usedAt'),
      cell: ({ row }) => {
        const usedAt = row.getValue('used_at') as string | null
        if (!usedAt) return '-'
        try {
          const date = new Date(usedAt)
          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 '-'
        }
      },
    },
  ]
}
