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 { Department } from '@/types/department';

export type { Department } from '@/types/department';

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

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

    const columns: ColumnDef<Department>[] = [
        {
            accessorKey: 'code',
            header: () => <span>Kode</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('code') as string}
                </span>
            ),
        },
        {
            accessorKey: 'name',
            header: () => <span>Nama</span>,
            cell: ({ row }) => <span>{row.getValue('name') as string}</span>,
        },
        {
            accessorKey: 'degree_level',
            header: () => <span className="block text-center">Jenjang</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const degreeLevel = row.getValue('degree_level') as
                    string | null;

                return (
                    <div className="flex justify-center">
                        {degreeLevel ? (
                            <Badge variant="secondary">{degreeLevel}</Badge>
                        ) : (
                            <span className="text-muted-foreground">-</span>
                        )}
                    </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 department = row.original;

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

    return columns;
}
