import { Eye } from 'iconsax-react'
import type { ColumnType } from '@/components/types/TableTypes'
import type { Report } from '../types/Types'
import { formatOptionalDate } from '@/helpers/func'
import Status from '@/components/Status'

interface GetReportTableColumnsProps {
    onViewContent?: (content: string) => void
}

const getStatusVariant = (status: string): 'success' | 'error' | 'warning' | 'info' | 'pending' => {
    const variantMap: Record<string, 'success' | 'error' | 'warning' | 'info' | 'pending'> = {
        'pending': 'pending',
        'read': 'info',
        'resolved': 'success',
        'rejected': 'error',
    }
    return variantMap[status] || 'pending'
}

export const getReportTableColumns = (props?: GetReportTableColumnsProps): ColumnType<Report>[] => {
    return [
        {
            key: 'subject',
            title: 'موضوع',
            render: (item: Report) => {
                return (
                    <div className="max-w-xs truncate" title={item.subject}>
                        {item.subject || '-'}
                    </div>
                )
            }
        },
        {
            key: 'content',
            title: 'محتوا',
            render: (item: Report) => {
                return (
                    <div className="max-w-xs truncate" title={item.content}>
                        {item.content || '-'}
                    </div>
                )
            }
        },
        {
            key: 'phone',
            title: 'شماره تماس',
            render: (item: Report) => {
                return item.phone || '-'
            }
        },
        {
            key: 'status',
            title: 'وضعیت',
            render: (item: Report) => {
                const statusLabels: Record<string, string> = {
                    'pending': 'در انتظار',
                    'read': 'خوانده شده',
                    'resolved': 'حل شده',
                    'rejected': 'رد شده',
                }
                return (
                    <Status
                        variant={getStatusVariant(item.status)}
                        label={statusLabels[item.status] || item.status}
                    />
                )
            }
        },
        {
            key: 'createdAt',
            title: 'تاریخ ثبت',
            render: (item: Report) => {
                return formatOptionalDate(item.createdAt, {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                })
            }
        },
        {
            key: 'details',
            title: '',
            render: (item: Report) => {
                if (!item.content || !props?.onViewContent) {
                    return null
                }
                return (
                    <Eye
                        size={20}
                        color="#8C90A3"
                        className="cursor-pointer hover:opacity-70 transition-opacity"
                        onClick={(e) => {
                            e.stopPropagation()
                            props.onViewContent?.(item.content)
                        }}
                    />
                )
            }
        },
    ]
}