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

import { Check, Eye, MoreHorizontal, Play, Trash2, X } from 'lucide-vue-next'
import { h } from 'vue'

import type { Ticket } from '@/pages/admin/tickets/data/schema'

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

export function getColumns(actions: {
  onUpdateStatus: (row: Ticket, status: string) => void
  onViewDetail: (row: Ticket) => void
  onDelete: (row: Ticket) => void
}, t: Composer['t']): ColumnDef<Ticket>[] {
  return [
    {
      accessorKey: 'type',
      header: () => t('admin.tickets.columns.ticketType'),
      cell: ({ row }) => {
        const ticketType = row.getValue('type') as string
        const typeMap: Record<string, { text: string, variant: any }> = {
          system: { text: t('admin.tickets.create.ticketTypeSystem'), variant: 'secondary' },
          application: { text: t('admin.tickets.create.ticketTypeApplication'), variant: 'default' },
        }
        const { text, variant } = typeMap[ticketType] || { text: ticketType, variant: 'default' }
        return h(Badge, { variant }, () => text)
      },
    },
    {
      accessorKey: 'title',
      header: () => t('admin.tickets.columns.title'),
      cell: ({ row }) => {
        const title = row.getValue('title') as string
        return h('div', { class: 'font-medium' }, title)
      },
    },
    {
      accessorKey: 'priority',
      header: () => t('admin.tickets.columns.priority'),
      cell: ({ row }) => {
        const priority = row.getValue('priority') as string
        const priorityMap: Record<string, { text: string, variant: any }> = {
          low: { text: t('admin.tickets.priority.low'), variant: 'secondary' },
          normal: { text: t('admin.tickets.priority.normal'), variant: 'default' },
          high: { text: t('admin.tickets.priority.high'), variant: 'destructive' },
          urgent: { text: t('admin.tickets.priority.urgent'), variant: 'destructive' },
        }
        const { text, variant } = priorityMap[priority] || { text: priority, variant: 'default' }
        return h(Badge, { variant }, () => text)
      },
    },
    {
      accessorKey: 'status',
      header: () => t('admin.tickets.columns.status'),
      cell: ({ row }) => {
        const status = row.getValue('status') as string
        const statusMap: Record<string, { text: string, variant: any }> = {
          open: { text: t('admin.tickets.status.open'), variant: 'destructive' },
          processing: { text: t('admin.tickets.status.processing'), variant: 'secondary' },
          resolved: { text: t('admin.tickets.status.resolved'), variant: 'default' },
          closed: { text: t('admin.tickets.status.closed'), variant: 'outline' },
        }
        const { text, variant } = statusMap[status] || { text: status, variant: 'default' }
        return h(Badge, { variant }, () => text)
      },
    },
    {
      accessorKey: 'app_name',
      header: () => t('admin.tickets.columns.application'),
      cell: ({ row }) => {
        const appName = row.getValue('app_name') as string
        return appName || '-'
      },
    },
    {
      accessorKey: 'user_name',
      header: () => t('admin.tickets.columns.submitter'),
      cell: ({ row }) => {
        const userName = row.getValue('user_name') as string
        return userName || '-'
      },
    },
    {
      accessorKey: 'created_at',
      header: () => t('admin.tickets.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.tickets.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 ticket = 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' }, t('common.openMenu')),
                ])),
              h(
                DropdownMenuContent,
                { align: 'end' },
                () => [
                  h(DropdownMenuItem, { onClick: () => actions.onViewDetail(ticket) }, () => [
                    h(Eye, { class: 'mr-2 h-4 w-4' }),
                    t('admin.tickets.actions.viewDetail'),
                  ]),
                  ticket.status === 'open'
                    ? h(DropdownMenuItem, { onClick: () => actions.onUpdateStatus(ticket, 'processing') }, () => [
                        h(Play, { class: 'mr-2 h-4 w-4' }),
                        t('admin.tickets.actions.startProcess'),
                      ])
                    : null,
                  ticket.status === 'processing'
                    ? h(DropdownMenuItem, { onClick: () => actions.onUpdateStatus(ticket, 'resolved') }, () => [
                        h(Check, { class: 'mr-2 h-4 w-4' }),
                        t('admin.tickets.actions.markResolved'),
                      ])
                    : null,
                  ticket.status === 'resolved'
                    ? h(DropdownMenuItem, { onClick: () => actions.onUpdateStatus(ticket, 'closed') }, () => [
                        h(X, { class: 'mr-2 h-4 w-4' }),
                        t('admin.tickets.actions.closeTicket'),
                      ])
                    : null,
                  h(DropdownMenuItem, { class: 'text-destructive', onClick: () => actions.onDelete(ticket) }, () => [
                    h(Trash2, { class: 'mr-2 h-4 w-4' }),
                    t('admin.tickets.actions.delete'),
                  ]),
                ].filter(Boolean),
              ),
            ],
          },
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]
}
