import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { AttachmentPreviewDialog } from '@/components/attachment-preview-dialog';
import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
import type { Material } from '@/types/material';

export type { Material } from '@/types/material';

type CreateColumnsParams = {
    handleEdit: (material: Material) => void;
    handleDeleteClick: (material: Material) => void;
    canUpdate: boolean;
    canDelete: boolean;
};

export function createMaterialColumns(
    params: CreateColumnsParams,
): ColumnDef<Material>[] {
    const { handleEdit, handleDeleteClick, canUpdate, canDelete } = params;

    const columns: ColumnDef<Material>[] = [
        {
            accessorKey: 'meeting_number',
            header: () => <span className="block text-center">Pertemuan</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const meetingNumber = row.getValue('meeting_number') as
                    number | null;

                return (
                    <div className="flex justify-center">
                        {meetingNumber ? (
                            <Badge variant="secondary">{meetingNumber}</Badge>
                        ) : (
                            <span className="text-muted-foreground">-</span>
                        )}
                    </div>
                );
            },
        },
        {
            accessorKey: 'title',
            header: () => <span>Judul</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('title') as string}
                </span>
            ),
        },
        {
            accessorKey: 'course_class.course.name',
            header: () => <span>Kelas</span>,
            cell: ({ row }) => {
                const courseClass = row.original.course_class;

                if (!courseClass) {
                    return '-';
                }

                return `${courseClass.course?.code ?? ''} ${courseClass.course?.name ?? ''}`;
            },
        },
    ];

    columns.push({
        id: 'actions',
        header: () => <span className="block text-center">Aksi</span>,
        meta: {
            className: 'w-[130px] text-center',
            headerClassName: 'w-[130px] text-center',
        },
        cell: ({ row }) => {
            const material = row.original;

            return (
                <div className="flex items-center justify-center">
                    {material.file_url && material.file_name && (
                        <AttachmentPreviewDialog
                            fileUrl={material.file_url}
                            fileName={material.file_name}
                            variant="icon"
                        />
                    )}
                    <RowActions
                        actions={[
                            {
                                label: 'Edit',
                                icon: <Pencil className="h-4 w-4" />,
                                show: canUpdate,
                                onClick: () => handleEdit(material),
                            },
                            {
                                label: 'Hapus',
                                icon: (
                                    <Trash2 className="h-4 w-4 text-destructive" />
                                ),
                                show: canDelete,
                                onClick: () => handleDeleteClick(material),
                            },
                        ]}
                    />
                </div>
            );
        },
    });

    return columns;
}
