import type { ColumnDef } from '@tanstack/react-table';
import { format } from 'date-fns';
import { ClipboardList, Pencil, Trash2, Upload } from 'lucide-react';
import { AttachmentPreviewDialog } from '@/components/attachment-preview-dialog';
import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
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;
    handleSubmit: (assignment: Assignment) => void;
    canUpdate: boolean;
    canDelete: boolean;
    canViewSubmissions: boolean;
    canSubmit: boolean;
};

export function createAssignmentColumns(
    params: CreateColumnsParams,
): ColumnDef<Assignment>[] {
    const {
        handleEdit,
        handleDeleteClick,
        handleSubmit,
        canUpdate,
        canDelete,
        canViewSubmissions,
        canSubmit,
    } = 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}
                    />
                );
            },
        },
        canSubmit
            ? {
                  id: 'my_submission_status',
                  header: () => (
                      <span className="block text-center">Status Saya</span>
                  ),
                  meta: {
                      className: 'w-[160px] text-center',
                      headerClassName: 'w-[160px] text-center',
                  },
                  cell: ({ row }) => {
                      const mySubmission = row.original.submissions?.[0];

                      return (
                          <div className="flex justify-center">
                              <Badge
                                  variant={
                                      mySubmission?.status === 'submitted'
                                          ? 'default'
                                          : 'secondary'
                                  }
                              >
                                  {mySubmission?.status === 'submitted'
                                      ? 'Sudah Mengumpulkan'
                                      : 'Belum Mengumpulkan'}
                              </Badge>
                          </div>
                      );
                  },
              }
            : {
                  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 || canSubmit) {
        columns.push({
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[150px] text-center',
                headerClassName: 'w-[150px] text-center',
            },
            cell: ({ row }) => {
                const assignment = row.original;
                const mySubmission = assignment.submissions?.[0];

                return (
                    <RowActions
                        actions={[
                            {
                                label:
                                    mySubmission?.status === 'submitted'
                                        ? 'Kumpulkan Ulang'
                                        : 'Kumpulkan Tugas',
                                icon: <Upload className="h-4 w-4" />,
                                show: canSubmit,
                                onClick: () => handleSubmit(assignment),
                            },
                            {
                                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;
}
