import type { ColumnDef } from '@tanstack/react-table';
import { History, 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;
    handleViewHistory: (department: Department) => void;
    canUpdate: boolean;
    canDelete: boolean;
};

export function createDepartmentColumns(
    params: CreateColumnsParams,
): ColumnDef<Department>[] {
    const {
        handleEdit,
        handleDeleteClick,
        handleViewHistory,
        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>
                );
            },
        },
        {
            id: 'kaprodi',
            header: () => <span>Kaprodi</span>,
            cell: ({ row }) => {
                const leader = row.original.current_leader;

                return leader ? (
                    <span>
                        {leader.lecturer?.user?.profile?.full_name ?? 'N/A'}
                    </span>
                ) : (
                    <span className="text-muted-foreground">-</span>
                );
            },
        },
    ];

    {
        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: 'Riwayat Kaprodi',
                                icon: <History className="h-4 w-4" />,
                                onClick: () => handleViewHistory(department),
                            },
                            {
                                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;
}
