import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2, Users } from 'lucide-react';
import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
import { index as enrollmentsIndex } from '@/routes/admin/manage/course-classes/enrollments';
import { formatAcademicTermLabel } from '@/types/academic-term';
import { ClassMethodLabels } from '@/types/course-class';
import type { CourseClass } from '@/types/course-class';

export type { CourseClass } from '@/types/course-class';

type CreateColumnsParams = {
    handleEdit: (courseClass: CourseClass) => void;
    handleDeleteClick: (courseClass: CourseClass) => void;
    canUpdate: boolean;
    canDelete: boolean;
    canViewEnrollments: boolean;
};

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

    const columns: ColumnDef<CourseClass>[] = [
        {
            accessorKey: 'course.name',
            header: () => <span>Mata Kuliah</span>,
            cell: ({ row }) => {
                const course = row.original.course;

                return course ? `${course.code} - ${course.name}` : '-';
            },
        },
        {
            accessorKey: 'lecturer.user.profile.full_name',
            header: () => <span>Dosen Pengampu</span>,
            cell: ({ row }) =>
                row.original.lecturer?.user?.profile?.full_name ?? '-',
        },
        {
            accessorKey: 'academic_term.academic_year',
            header: () => <span>Periode</span>,
            cell: ({ row }) => {
                const term = row.original.academic_term;

                return term ? formatAcademicTermLabel(term) : '-';
            },
        },
        {
            accessorKey: 'method',
            header: () => <span className="block text-center">Metode</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const method = row.getValue('method') as
                    keyof typeof ClassMethodLabels | null;

                return (
                    <div className="flex justify-center">
                        {method ? (
                            <Badge variant="secondary">
                                {ClassMethodLabels[method]}
                            </Badge>
                        ) : (
                            <span className="text-muted-foreground">-</span>
                        )}
                    </div>
                );
            },
        },
        {
            accessorKey: 'enrollments_count',
            header: () => <span className="block text-center">Peserta</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => (
                <div className="text-center">
                    {row.original.enrollments_count}
                </div>
            ),
        },
    ];

    if (canViewEnrollments || 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 courseClass = row.original;

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

    return columns;
}
