import type { ColumnDef } from '@tanstack/react-table';
import { format } from 'date-fns';
import { Eye, Pencil, Trash2 } from 'lucide-react';
import { RowActions } from '@/components/row-actions';
import { StatusBadge } from '@/components/status-badge';
import { Badge } from '@/components/ui/badge';
import type { Feedback } from '@/types/feedback';
import { FeedbackTypeLabels } from '@/types/feedback';

export type { Feedback } from '@/types/feedback';

type StatusOption = { value: string; label: string };

type CreateColumnsParams = {
    handleView: (feedback: Feedback) => void;
    handleEdit: (feedback: Feedback) => void;
    handleDeleteClick: (feedback: Feedback) => void;
    handleStatusChange: (feedback: Feedback, status: string) => void;
    statuses: StatusOption[];
    canUpdateStatus: boolean;
    canUpdate: boolean;
    canDelete: boolean;
};

export const FeedbackStatusVariants: Record<
    string,
    'default' | 'secondary' | 'destructive' | 'outline'
> = {
    submitted: 'outline',
    in_review: 'secondary',
    resolved: 'default',
};

function stripHtml(html: string): string {
    return html
        .replace(/<[^>]+>/g, ' ')
        .replace(/\s+/g, ' ')
        .trim();
}

export function createFeedbackColumns(
    params: CreateColumnsParams,
): ColumnDef<Feedback>[] {
    const {
        handleView,
        handleEdit,
        handleDeleteClick,
        handleStatusChange,
        statuses,
        canUpdateStatus,
        canUpdate,
        canDelete,
    } = params;

    return [
        {
            accessorKey: 'subject',
            header: () => <span>Subjek</span>,
            cell: ({ row }) => {
                const feedback = row.original;

                return (
                    <div>
                        <p className="font-medium">{feedback.subject}</p>
                        <p className="line-clamp-1 text-xs text-muted-foreground">
                            {stripHtml(feedback.message)}
                        </p>
                    </div>
                );
            },
        },
        {
            accessorKey: 'type',
            header: () => <span className="block text-center">Jenis</span>,
            meta: {
                className: 'w-[110px] text-center',
                headerClassName: 'w-[110px] text-center',
            },
            cell: ({ row }) => (
                <div className="flex justify-center">
                    <Badge variant="outline">
                        {FeedbackTypeLabels[row.original.type]}
                    </Badge>
                </div>
            ),
        },
        {
            accessorKey: 'status',
            header: () => <span className="block text-center">Status</span>,
            meta: {
                className: 'w-[130px] text-center',
                headerClassName: 'w-[130px] text-center',
            },
            cell: ({ row }) => (
                <div className="flex justify-center">
                    <StatusBadge
                        status={row.original.status}
                        statuses={statuses}
                        variants={FeedbackStatusVariants}
                        onChange={(status) =>
                            handleStatusChange(row.original, status)
                        }
                        disabled={!canUpdateStatus}
                    />
                </div>
            ),
        },
        {
            accessorKey: 'handler.profile.full_name',
            header: () => <span>Ditindaklanjuti Oleh</span>,
            cell: ({ row }) => row.original.handler?.profile?.full_name ?? '-',
        },
        {
            accessorKey: 'created_at',
            header: () => <span>Dikirim</span>,
            cell: ({ row }) =>
                format(new Date(row.original.created_at), 'd MMM yyyy, HH:mm'),
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => (
                <RowActions
                    actions={[
                        {
                            label: 'Lihat Detail',
                            icon: <Eye className="h-4 w-4" />,
                            onClick: () => handleView(row.original),
                        },
                        {
                            label: 'Edit',
                            icon: <Pencil className="h-4 w-4" />,
                            show: canUpdate,
                            onClick: () => handleEdit(row.original),
                        },
                        {
                            label: 'Hapus',
                            icon: (
                                <Trash2 className="h-4 w-4 text-destructive" />
                            ),
                            show: canDelete,
                            onClick: () => handleDeleteClick(row.original),
                        },
                    ]}
                />
            ),
        },
    ];
}
