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

import { LogOut, MoreHorizontal } from 'lucide-vue-next'
import { h } from 'vue'

import type { Session } from '@/pages/admin/sessions/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: {
  onForceOffline: (row: Session) => void
}): ColumnDef<Session>[] {
  return [
    {
      accessorKey: 'instance_id',
      header: '实例标识',
      cell: ({ row }) => {
        const instanceId = row.getValue('instance_id') as string
        if (!instanceId)
          return '-'
        const displayText = instanceId.length > 16
          ? `${instanceId.substring(0, 8)}...${instanceId.substring(instanceId.length - 4)}`
          : instanceId
        return h('span', { class: 'font-mono text-xs' }, displayText)
      },
    },
    {
      accessorKey: 'device_identifier',
      header: '设备指纹',
      cell: ({ row }) => {
        const deviceId = row.getValue('device_identifier') as string
        if (!deviceId)
          return '-'
        const displayText = deviceId.length > 16
          ? `${deviceId.substring(0, 8)}...${deviceId.substring(deviceId.length - 4)}`
          : deviceId
        return h('span', { class: 'font-mono text-xs' }, displayText)
      },
    },
    {
      accessorKey: 'device_name',
      header: '设备名称',
      cell: ({ row }) => {
        const deviceName = row.getValue('device_name') as string
        return deviceName || '-'
      },
    },
    {
      accessorKey: 'username',
      header: '所属用户',
      cell: ({ row }) => {
        const username = row.getValue('username') as string
        return username || '-'
      },
    },
    {
      accessorKey: 'app_name',
      header: '所属应用',
      cell: ({ row }) => {
        const appName = row.getValue('app_name') as string
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'last_heartbeat',
      header: '最后心跳',
      cell: ({ row }) => {
        const lastHeartbeat = row.getValue('last_heartbeat')
        if (!lastHeartbeat)
          return '-'
        try {
          const date = new Date(lastHeartbeat 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',
            second: '2-digit',
          })
        }
        catch {
          return '-'
        }
      },
    },
    {
      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 session = 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, { class: 'text-destructive', onClick: () => actions.onForceOffline(session) }, () => [
                    h(LogOut, { class: 'mr-2 h-4 w-4' }),
                    '强制下线',
                  ]),
                ],
              ),
            ],
          },
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]
}
