import { Download, Edit, Trash2 } from 'lucide-vue-next'
import type { ColumnDef } from '@tanstack/vue-table'

import { h } from 'vue'

import type { Version } from '@/pages/admin/versions/data/schema'

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

interface ColumnOptions {
  onDownload: (row: Version) => void
  onEdit: (row: Version) => void
  onDelete: (row: Version) => void
}

function formatFileSize(bytes: number): string {
  if (bytes === 0)
    return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return `${Number.parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`
}

export function getColumns(options: ColumnOptions, t: (key: string) => string): ColumnDef<Version>[] {
  return [
    {
      accessorKey: 'version',
      header: () => t('admin.versions.columns.version'),
      cell: ({ row }) => {
        return h('code', { class: 'text-sm bg-muted px-2 py-1 rounded' }, `v${row.original.version}`)
      },
    },
    {
      accessorKey: 'application_name',
      header: () => t('admin.versions.columns.application'),
      cell: ({ row }) => {
        const appName = row.original.application_name
        return appName ? h(Badge, { variant: 'secondary' }, () => appName) : '-'
      },
    },
    {
      accessorKey: 'description',
      header: () => t('admin.versions.columns.description'),
      cell: ({ row }) => {
        return h('div', { class: 'max-w-xs truncate' }, row.original.description || '-')
      },
    },
    {
      accessorKey: 'file_size',
      header: () => t('admin.versions.columns.fileSize'),
      cell: ({ row }) => {
        return formatFileSize(row.original.file_size)
      },
    },
    {
      accessorKey: 'update_strategy',
      header: () => t('admin.versions.columns.strategy'),
      cell: ({ row }) => {
        const strategyClasses: Record<string, string> = {
          optional: 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400',
          forced: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400',
        }
        const strategyLabels: Record<string, string> = {
          optional: t('admin.versions.strategies.optional'),
          forced: t('admin.versions.strategies.forced'),
        }
        return h(Badge, { class: strategyClasses[row.original.update_strategy] }, () => strategyLabels[row.original.update_strategy])
      },
    },
    {
      accessorKey: 'update_method',
      header: () => t('admin.versions.columns.method'),
      cell: ({ row }) => {
        const methodClasses: Record<string, string> = {
          manual: 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400',
          auto: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400',
        }
        const methodLabels: Record<string, string> = {
          manual: t('admin.versions.methods.manual'),
          auto: t('admin.versions.methods.auto'),
        }
        return h(Badge, { class: methodClasses[row.original.update_method] }, () => methodLabels[row.original.update_method])
      },
    },
    {
      accessorKey: 'created_at',
      header: () => t('admin.versions.columns.createdAt'),
      cell: ({ row }) => {
        return h('span', { class: 'text-sm text-muted-foreground' }, new Date(row.original.created_at).toLocaleString('zh-CN'))
      },
    },
    {
      id: 'actions',
      header: () => t('admin.versions.columns.actions'),
      cell: ({ row }) => {
        const buttons = []
        if (row.original.file_path) {
          buttons.push(
            h(Button, {
              variant: 'ghost',
              size: 'sm',
              onClick: () => options.onDownload(row.original),
            }, () => [
              h(Download, { class: 'h-4 w-4' }),
            ]),
          )
        }
        buttons.push(
          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' }),
          ]),
        )
        return h('div', { class: 'flex items-center justify-end gap-1' }, buttons)
      },
    },
  ]
}
