import type { ColumnDef } from '@tanstack/react-table';
import { format } from 'date-fns';
import { ClipboardList, Pencil, Trash2 } from 'lucide-react';
import { AttachmentPreviewDialog } from '@/components/attachment-preview-dialog';
import { RowActions } from '@/components/row-actions';
import { index as submissionsIndex } from '@/routes/admin/academic-classes/assignments/submissions';
import type { Assignment } from '@/types/assignment';

export type { Assignment } from '@/types/assignment';

type CreateColumnsParams = {
    handleEdit: (assignment: Assignment) => void;
    handleDeleteClick: (assignment: Assignment) => void;
    canUpdate: boolean;
    canDelete: boolean;
    canViewSubmissions: boolean;
};

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

    const columns: ColumnDef<Assignment>[] = [
        {
            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 ?? ''}`;
            },
        },
        {
            accessorKey: 'deadline',
            header: () => <span>Batas Waktu</span>,
            cell: ({ row }) => {
                const deadline = row.getValue('deadline') as string;

                return format(new Date(deadline), 'd MMM yyyy, HH:mm');
            },
        },
        {
            accessorKey: 'attachment_name',
            header: () => <span>Lampiran</span>,
            cell: ({ row }) => {
                const assignment = row.original;

                if (!assignment.attachment_url || !assignment.attachment_name) {
                    return <span className="text-muted-foreground">-</span>;
                }

                return (
                    <AttachmentPreviewDialog
                        fileUrl={assignment.attachment_url}
                        fileName={assignment.attachment_name}
                    />
                );
            },
        },
        {
            accessorKey: 'submissions_count',
            header: () => (
                <span className="block text-center">Pengumpulan</span>
            ),
            meta: {
                className: 'w-[120px] text-center',
                headerClassName: 'w-[120px] text-center',
            },
            cell: ({ row }) => (
                <div className="text-center">
                    {row.original.submissions_count}
                </div>
            ),
        },
    ];

    if (canViewSubmissions || canUpdate || canDelete) {
        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 assignment = row.original;

                return (
                    <RowActions
                        actions={[
                            {
                                label: 'Pengumpulan',
                                icon: <ClipboardList className="h-4 w-4" />,
                                show: canViewSubmissions,
                                href: submissionsIndex.url(assignment.id),
                            },
                            {
                                label: 'Edit',
                                icon: <Pencil className="h-4 w-4" />,
                                show: canUpdate,
                                onClick: () => handleEdit(assignment),
                            },
                            {
                                label: 'Hapus',
                                icon: (
                                    <Trash2 className="h-4 w-4 text-destructive" />
                                ),
                                show: canDelete,
                                onClick: () => handleDeleteClick(assignment),
                            },
                        ]}
                    />
                );
            },
        });
    }

    return columns;
}
