import type { ColumnType } from '@/components/types/TableTypes'
import type { ICoupon } from '../types/Types'
import TrashWithConfrim from '@/components/TrashWithConfrim'
import { Eye } from 'iconsax-react'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'
import { formatOptionalDate, formatFaNumber } from '@/helpers/func'
import Status from '@/components/Status'

interface GetCouponTableColumnsParams {
    onDelete?: (id: string) => void
    isDeleting?: boolean
}

export const getCouponTableColumns = ({ onDelete, isDeleting }: GetCouponTableColumnsParams = {}): ColumnType<ICoupon>[] => {
    return [
        {
            key: 'code',
            title: 'کد کوپن',
            render: (item: ICoupon) => {
                return item.code || '-'
            }
        },
        {
            key: 'name',
            title: 'نام',
            render: (item: ICoupon) => {
                return item.name || '-'
            }
        },
        {
            key: 'type',
            title: 'نوع',
            render: (item: ICoupon) => {
                return item.type === 'PERCENTAGE' ? 'درصدی' : 'مقدار ثابت'
            }
        },
        {
            key: 'value',
            title: 'مقدار',
            render: (item: ICoupon) => {
                if (item.type === 'PERCENTAGE') {
                    return `${formatFaNumber(item.value)}%`
                }
                return `${formatFaNumber(item.value)} تومان`
            }
        },
        {
            key: 'usedCount',
            title: 'استفاده شده',
            render: (item: ICoupon) => {
                return `${formatFaNumber(item.usedCount)}/${formatFaNumber(item.maxUses)}`
            }
        },
        {
            key: 'startDate',
            title: 'تاریخ شروع',
            render: (item: ICoupon) => {
                return formatOptionalDate(item.startDate)
            }
        },
        {
            key: 'endDate',
            title: 'تاریخ پایان',
            render: (item: ICoupon) => {
                return formatOptionalDate(item.endDate)
            }
        },
        {
            key: 'isActive',
            title: 'وضعیت',
            render: (item: ICoupon) => {
                return (
                    <Status
                        variant={item.isActive ? 'active' : 'inactive'}
                        label={item.isActive ? 'فعال' : 'غیرفعال'}
                    />
                )
            }
        },
        {
            key: 'actions',
            title: '',
            render: (item: ICoupon) => {
                return (
                    <div className='flex gap-2 items-center'>
                        <Link to={Pages.coupons.update + item.id}>
                            <Eye
                                size={20}
                                color='#8C90A3'
                                className='cursor-pointer'
                            />
                        </Link>
                        {onDelete && (
                            <TrashWithConfrim
                                onDelete={() => onDelete(item.id)}
                                isloading={isDeleting}
                            />
                        )}
                    </div>
                )
            }
        },
    ]
}

