import { Eye } from 'iconsax-react'
import type { ColumnType } from '@/components/types/TableTypes'
import type { Product } from '../types/Types'
import { formatPrice } from '../utils/formatters'
import Status from '@/components/Status'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'
import TrashWithConfrim from '@/components/TrashWithConfrim'

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

export const getProductTableColumns = ({ onDelete, isDeleting }: GetProductTableColumnsParams): ColumnType<Product>[] => {

    return [
        {
            key: 'image',
            title: 'تصویر',
            render: (item: Product) => {
                const imageUrl = item.images && item.images.length > 0 ? item.images[0] : ''
                return (
                    <div className='w-10 h-10 rounded-full overflow-hidden bg-gray-200'>
                        {imageUrl ? (
                            <img src={imageUrl} alt={item.title} className='w-full h-full object-cover' />
                        ) : (
                            <div className='w-full h-full flex items-center justify-center text-gray-400 text-xs'>
                                بدون تصویر
                            </div>
                        )}
                    </div>
                )
            }
        },
        {
            key: 'title',
            title: 'نام کالا',
        },
        {
            key: 'categories',
            title: 'دسته بندی',
            render: (item: Product) => {
                return item.category?.title
                    ? item.category.title
                    : '-'
            }
        },
        {
            key: 'price',
            title: 'قیمت',
            render: (item: Product) => {
                const variants = item.variants
                if (variants && variants.length > 0) {
                    const prices = variants.map((v) => v.price).filter((p) => p != null)
                    if (prices.length === 0) return formatPrice(item.price)
                    const minPrice = Math.min(...prices)
                    const maxPrice = Math.max(...prices)
                    if (minPrice === maxPrice) return formatPrice(minPrice)
                    return (
                        <span>
                            از {formatPrice(minPrice)} تا {formatPrice(maxPrice)}
                        </span>
                    )
                }
                return formatPrice(item.price)
            }
        },
        {
            key: 'isActive',
            title: 'وضعیت',
            render: (item: Product) => {
                return (
                    <Status variant={item.isActive ? 'success' : 'error'} label={item.isActive ? 'فعال' : 'غیرفعال'} />
                )
            }
        },
        {
            key: 'view',
            title: '',
            render: (item: Product) => {
                return (
                    <div className='flex gap-2 items-center'>
                        <Link to={Pages.products.update + item.id} className='flex gap-2 items-center'>
                            <Eye size={20} color='#8C90A3' />
                        </Link>
                        <TrashWithConfrim
                            onDelete={() => onDelete(item.id)}
                            isloading={isDeleting}
                        />
                    </div>
                )
            }
        },
    ]
}
