'use client'

import {
  DataTableColumnDatetime,
  DataTableColumnCurrency,
  DataTableColumnHeaderSort
} from '@repo/ui/components/data-table'
import { type ColumnDef } from '@tanstack/react-table'
import { HelpCircleIcon } from 'lucide-react'

import { cn } from '@repo/ui/lib/utils'
import { Badge } from '@repo/ui/components/ui/badge'

import { labels, methods, statuses, type Order } from './data'

export type { Order }

export const columns: ColumnDef<Order>[] = [
  {
    accessorKey: 'payment_method',
    header: 'Forma de pag.',
    cell: ({ row }) => {
      const paymentMethod = row.getValue('payment_method') as string
      return <>{methods[paymentMethod] ?? paymentMethod}</>
    }
  },
  {
    accessorKey: 'status',
    header: 'Status',
    cell: ({ row }) => {
      const s = row.getValue('status') as string
      const status = labels[s] ?? s
      const { icon: Icon, color } = statuses?.[s] ?? { icon: HelpCircleIcon }

      return (
        <Badge variant="outline" className={cn(color, ' px-1.5')}>
          <Icon className={cn('stroke-2', color)} />
          {status}
        </Badge>
      )
    }
  },
  {
    accessorKey: 'total',
    header: 'Valor total',
    cell: DataTableColumnCurrency
  },
  {
    accessorKey: 'create_date',
    enableSorting: true,
    meta: { title: 'Comprado em' },
    header: DataTableColumnHeaderSort,
    cell: DataTableColumnDatetime
  },
  {
    accessorKey: 'due_date',
    enableSorting: true,
    meta: { title: 'Vencimento em' },
    header: DataTableColumnHeaderSort,
    cell: DataTableColumnDatetime
  },
  {
    accessorKey: 'payment_date',
    enableSorting: true,
    meta: { title: 'Pago em' },
    header: DataTableColumnHeaderSort,
    cell: DataTableColumnDatetime
  }
]
