import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { ColumnDef } from '@tanstack/react-table'; import { format, isFuture, isPast, parseISO } from 'date-fns'; import { Copy, Pencil } from 'lucide-react'; import CouponForm from './coupon-form'; interface CouponTableColumnsProps { exams: Exam[]; } const CouponTableColumns = ({ exams }: CouponTableColumnsProps): ColumnDef[] => { const getCouponStatus = (coupon: ExamCoupon) => { if (!coupon.is_active) return { label: 'Inactive', variant: 'secondary' as const }; if (coupon.valid_to && isPast(parseISO(coupon.valid_to))) return { label: 'Expired', variant: 'destructive' as const }; if (coupon.valid_from && isFuture(parseISO(coupon.valid_from))) return { label: 'Scheduled', variant: 'secondary' as const }; if (coupon.usage_limit && coupon.used_count >= coupon.usage_limit) return { label: 'Used Up', variant: 'destructive' as const }; return { label: 'Active', variant: 'default' as const }; }; const copyCouponCode = (code: string) => { navigator.clipboard.writeText(code); alert('Coupon code copied to clipboard!'); }; return [ { accessorKey: 'code', header: () =>

Coupon Code

, cell: ({ row }) => (
{row.original.code}
), }, { accessorKey: 'discount', header: 'Discount', cell: ({ row }) => ( {row.original.discount_type === 'percentage' ? `${row.original.discount}% OFF` : `$${row.original.discount} OFF`} ), }, { accessorKey: 'exam', header: 'Exam', cell: ({ row }) => row.original.exam ? ( {row.original.exam.title} ) : ( Global Coupon ), }, { accessorKey: 'usage', header: 'Usage', cell: ({ row }) => { const limited = row.original.usage_type === 'limited'; return limited ? ( {row.original.used_count} / {row.original.usage_limit} ) : ( Unlimited ); }, }, { accessorKey: 'valid_from', header: 'Valid From', cell: ({ row }) => (row.original.valid_from ? format(parseISO(row.original.valid_from), 'MMM dd, yyyy HH:mm') : '-'), }, { accessorKey: 'valid_to', header: 'Valid To', cell: ({ row }) => (row.original.valid_to ? format(parseISO(row.original.valid_to), 'MMM dd, yyyy HH:mm') : '-'), }, { accessorKey: 'status', header: 'Status', cell: ({ row }) => { const status = getCouponStatus(row.original); return {status.label}; }, }, { id: 'actions', header: () =>

Actions

, cell: ({ row }) => { const coupon = row.original; return (
} />
); }, }, ]; }; export default CouponTableColumns;