import type { ColumnType } from '@/components/types/TableTypes'
import type { Pager } from '../types/Types'
import { formatOptionalDate } from '@/helpers/func'
import Status from '@/components/Status'
import { Eye } from 'iconsax-react'
import { PagerStatus } from '../enum/Enum'
import type { TFunction } from 'i18next'

interface GetPagerTableColumnsProps {
    onViewDetails: (pager: Pager) => void
    t: TFunction
}

const getStatusVariant = (status: string) => {
    switch (status) {
        case PagerStatus.Pending:
            return 'warning'
        case PagerStatus.Acknowledged:
            return 'info'
        case PagerStatus.Rejected:
            return 'error'
        case PagerStatus.Completed:
            return 'success'
        default:
            return 'pending'
    }
}

export const getPagerTableColumns = (props?: GetPagerTableColumnsProps): ColumnType<Pager>[] => {
    const getStatusLabel = (status: string) => {
        if (!props?.t) return status
        switch (status) {
            case PagerStatus.Pending:
                return props.t('pager.status.pending')
            case PagerStatus.Acknowledged:
                return props.t('pager.status.acknowledged')
            case PagerStatus.Rejected:
                return props.t('pager.status.rejected')
            case PagerStatus.Completed:
                return props.t('pager.status.completed')
            default:
                return status
        }
    }

    return [
        {
            key: 'tableNumber',
            title: 'شماره میز',
            render: (item: Pager) => {
                return item.tableNumber || '-'
            }
        },
        {
            key: 'message',
            title: 'پیام',
            render: (item: Pager) => {
                const message = item.message || '-'
                const truncatedMessage = message.length > 50 ? `${message.substring(0, 50)}...` : message
                return (
                    <div className="max-w-xs truncate" title={message}>
                        {truncatedMessage}
                    </div>
                )
            }
        },
        {
            key: 'status',
            title: 'وضعیت',
            render: (item: Pager) => {
                const statusLabel = getStatusLabel(item.status)
                const statusVariant = getStatusVariant(item.status)
                return (
                    <Status variant={statusVariant} label={statusLabel} />
                )
            }
        },
        {
            key: 'createdAt',
            title: 'تاریخ ایجاد',
            render: (item: Pager) => {
                return formatOptionalDate(item.createdAt, {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                })
            }
        },
        {
            key: 'updatedAt',
            title: 'آخرین بروزرسانی',
            render: (item: Pager) => {
                return formatOptionalDate(item.updatedAt, {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                })
            }
        },
        {
            key: 'details',
            title: '',
            render: (item: Pager) => {
                return (
                    <button
                        onClick={() => props?.onViewDetails(item)}
                        className="cursor-pointer hover:opacity-70 transition-opacity"
                    >
                        <Eye size={20} color="#8C90A3" />
                    </button>
                )
            }
        },
    ]
}
