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

interface GetOrderTableColumnsParams {
    onDelete?: (id: string) => void
    isDeleting?: boolean
    t: TFunction
}

const getStatusVariant = (status: string): 'success' | 'error' | 'warning' | 'info' | 'pending' => {
    const variantMap: Record<string, 'success' | 'error' | 'warning' | 'info' | 'pending'> = {
        'pendingPayment': 'pending',
        'paid': 'info',
        'preparing': 'warning',
        'ready': 'info',
        'shipped': 'info',
        'deliveredToRecepient': 'success',
        'completed': 'success',
        'canceled': 'error',
    }
    return variantMap[status] || 'pending'
}

export const getOrderTableColumns = ({ t }: GetOrderTableColumnsParams): ColumnType<Order>[] => {
    return [
        {
            key: 'orderNumber',
            title: 'شماره سفارش',
            render: (item: Order) => {
                return formatFaNumber(item.orderNumber)
            }
        },
        {
            key: 'customerName',
            title: 'نام مشتری',
            render: (item: Order) => {
                const name = [item.user.firstName, item.user.lastName].filter(Boolean).join(' ')
                return name || '-'
            }
        },
        {
            key: 'total',
            title: 'مبلغ سفارش',
            render: (item: Order) => {
                return formatPrice(item.total)
            }
        },
        {
            key: 'createdAt',
            title: 'زمان سفارش',
            render: (item: Order) => {
                return formatOptionalDate(item.createdAt, {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                })
            }
        },
        {
            key: 'deliveryMethod',
            title: 'نوع تحویل',
            render: (item: Order) => {
                return item.deliveryMethod?.description || '-'
            }
        },
        {
            key: 'paymentMethod',
            title: 'نوع پرداخت',
            render: (item: Order) => {
                return item.paymentMethod?.description || '-'
            }
        },
        {
            key: 'status',
            title: 'وضعیت',
            render: (item: Order) => {
                return (
                    <Status
                        variant={getStatusVariant(item.status)}
                        label={t(`order_status.${item.status}`)}
                    />
                )
            }
        },
        {
            key: 'details',
            title: '',
            render: (item: Order) => {
                return (
                    <Link to={Pages.orders.detail + item.id}>
                        <Eye size={20} color="#8C90A3" className="cursor-pointer" />
                    </Link>
                )
            }
        },
    ]
}
