import { Cloud, Edit, Folder, Globe, HardDrive, Lock, Star, Trash2 } from 'lucide-vue-next'
import type { ColumnDef } from '@tanstack/vue-table'

import { h } from 'vue'

import type { StorageConfig } from '@/pages/admin/storage-configs/data/schema'

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

interface ColumnOptions {
  onToggleStatus: (row: StorageConfig) => void
  onSetDefault: (row: StorageConfig) => void
  onEdit: (row: StorageConfig) => void
  onDelete: (row: StorageConfig) => void
}

export function getColumns(options: ColumnOptions, t: (key: string) => string): ColumnDef<StorageConfig>[] {
  const typeLabels: Record<string, string> = {
    local: '本地存储',
    s3: 'S3存储',
    webdav: 'WebDAV',
    ftp: 'FTP',
    sftp: 'SFTP',
  }

  const typeIcons: Record<string, any> = {
    local: HardDrive,
    s3: Cloud,
    webdav: Globe,
    ftp: Folder,
    sftp: Lock,
  }

  return [
    {
      accessorKey: 'name',
      header: () => '名称',
      cell: ({ row }) => {
        return h('div', { class: 'flex items-center gap-2' }, [
          h(typeIcons[row.original.type] || HardDrive, { class: 'h-4 w-4 text-muted-foreground' }),
          h('span', { class: 'font-medium' }, row.original.name),
          row.original.is_default
            ? h(Badge, { variant: 'secondary', class: 'text-xs' }, () => '默认')
            : null,
        ])
      },
    },
    {
      accessorKey: 'type',
      header: () => '类型',
      cell: ({ row }) => {
        return h(Badge, { variant: 'outline' }, () => typeLabels[row.original.type] || row.original.type)
      },
    },
    {
      accessorKey: 'endpoint',
      header: () => '端点/地址',
      cell: ({ row }) => {
        return h('span', { class: 'text-sm text-muted-foreground' }, row.original.endpoint || '-')
      },
    },
    {
      accessorKey: 'status',
      header: () => '状态',
      cell: ({ row }) => {
        const statusLabels: Record<string, string> = {
          active: '启用',
          inactive: '禁用',
        }
        const statusClasses: Record<string, string> = {
          active: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400 cursor-pointer',
          inactive: 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400 cursor-pointer',
        }
        return h(Badge, {
          class: statusClasses[row.original.status],
          onClick: () => options.onToggleStatus(row.original),
        }, () => statusLabels[row.original.status])
      },
    },
    {
      accessorKey: 'remark',
      header: () => '备注',
      cell: ({ row }) => {
        return h('span', { class: 'text-sm text-muted-foreground truncate max-w-[200px] block' }, row.original.remark || '-')
      },
    },
    {
      id: 'actions',
      header: () => '操作',
      cell: ({ row }) => {
        const isLocal = row.original.type === 'local'
        return h('div', { class: 'flex items-center justify-end gap-1' }, [
          !row.original.is_default
            ? h(Button, {
                variant: 'ghost',
                size: 'sm',
                onClick: () => options.onSetDefault(row.original),
                title: '设为默认',
              }, () => [
                h(Star, { class: 'h-4 w-4' }),
              ])
            : null,
          h(Button, {
            variant: 'ghost',
            size: 'sm',
            onClick: () => options.onEdit(row.original),
          }, () => [
            h(Edit, { class: 'h-4 w-4' }),
          ]),
          !isLocal
            ? h(Button, {
                variant: 'ghost',
                size: 'sm',
                onClick: () => options.onDelete(row.original),
              }, () => [
                h(Trash2, { class: 'h-4 w-4 text-destructive' }),
              ])
            : null,
        ])
      },
    },
  ]
}
