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

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

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

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

    const columns: ColumnDef<Course>[] = [
        {
            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: '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>
            ),
        },
    ];

    if (canUpdate || canDelete) {
        columns.push({
            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" />,
                                show: canUpdate,
                                onClick: () => handleEdit(course),
                            },
                            {
                                label: 'Hapus',
                                icon: (
                                    <Trash2 className="h-4 w-4 text-destructive" />
                                ),
                                show: canDelete,
                                onClick: () => handleDeleteClick(course),
                            },
                        ]}
                    />
                );
            },
        });
    }

    return columns;
}
