import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
import type { Course } from '@/types/course';

export type { Course } from '@/types/course';

type CreateColumnsParams = {
    handleEdit: (course: Course) => void;
    handleDeleteClick: (course: Course) => void;
};

export function createCourseColumns(
    params: CreateColumnsParams,
): ColumnDef<Course>[] {
    const { handleEdit, handleDeleteClick } = params;

    return [
        {
            accessorKey: 'code',
            header: () => <span>Kode</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('code') as string}
                </span>
            ),
        },
        {
            accessorKey: 'name',
            header: () => <span>Nama Mata Kuliah</span>,
            cell: ({ row }) => <span>{row.getValue('name') as string}</span>,
        },
        {
            accessorKey: 'department.name',
            header: () => <span>Jurusan</span>,
            cell: ({ row }) => row.original.department?.name ?? '-',
        },
        {
            accessorKey: 'credits',
            header: () => <span className="block text-center">SKS</span>,
            meta: {
                className: 'w-[80px] text-center',
                headerClassName: 'w-[80px] text-center',
            },
            cell: ({ row }) => (
                <div className="text-center">
                    {row.getValue('credits') as number}
                </div>
            ),
        },
        {
            accessorKey: 'semester_number',
            header: () => <span className="block text-center">Semester</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const semesterNumber = row.getValue('semester_number') as
                    number | null;

                return (
                    <div className="flex justify-center">
                        {semesterNumber ? (
                            <Badge variant="secondary">{semesterNumber}</Badge>
                        ) : (
                            <span className="text-muted-foreground">-</span>
                        )}
                    </div>
                );
            },
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const course = row.original;

                return (
                    <RowActions
                        actions={[
                            {
                                label: 'Edit',
                                icon: <Pencil className="h-4 w-4" />,
                                onClick: () => handleEdit(course),
                            },
                            {
                                label: 'Hapus',
                                icon: (
                                    <Trash2 className="h-4 w-4 text-destructive" />
                                ),
                                onClick: () => handleDeleteClick(course),
                            },
                        ]}
                    />
                );
            },
        },
    ];
}
