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

export type Lecturer = {
    id: number;
    username: string;
    email: string;
    profile: { full_name: string } | null;
    lecturer: { lecturer_number: string; department: { name: string } | null } | null;
};

type CreateColumnsParams = {
    handleEdit: (lecturer: Lecturer) => void;
    handleDeleteClick: (lecturer: Lecturer) => void;
    handleResetPassword: (lecturer: Lecturer) => void;
};

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

    return [
        {
            accessorKey: 'lecturer.lecturer_number',
            header: () => <span>NIDN</span>,
            cell: ({ row }) => row.original.lecturer?.lecturer_number ?? '-',
        },
        {
            accessorKey: 'profile.full_name',
            header: () => <span>Nama Lengkap</span>,
            cell: ({ row }) => row.original.profile?.full_name ?? '-',
        },
        {
            id: 'account',
            header: () => <span>Akun</span>,
            cell: ({ row }) => (
                <div className="flex flex-col">
                    <span>{row.original.username}</span>
                    <span className="text-sm text-muted-foreground">{row.original.email}</span>
                </div>
            ),
        },
        {
            accessorKey: 'lecturer.department.name',
            header: () => <span>Jurusan</span>,
            cell: ({ row }) => row.original.lecturer?.department?.name ?? '-',
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const lecturer = row.original;

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