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

export type { Material } from '@/types/material';

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

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

    const columns: ColumnDef<Material>[] = [
        {
            accessorKey: 'meeting_number',
            header: () => <span className="block text-center">Pertemuan</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const meetingNumber = row.getValue('meeting_number') as
                    number | null;

                return (
                    <div className="flex justify-center">
                        {meetingNumber ? (
                            <Badge variant="secondary">{meetingNumber}</Badge>
                        ) : (
                            <span className="text-muted-foreground">-</span>
                        )}
                    </div>
                );
            },
        },
        {
            accessorKey: 'title',
            header: () => <span>Judul</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('title') as string}
                </span>
            ),
        },
        {
            accessorKey: 'course_class.course.name',
            header: () => <span>Kelas</span>,
            cell: ({ row }) => {
                const courseClass = row.original.course_class;

                if (!courseClass) {
                    return '-';
                }

                return `${courseClass.course?.code ?? ''} ${courseClass.course?.name ?? ''}`;
            },
        },
        {
            accessorKey: 'file_name',
            header: () => <span>File</span>,
            cell: ({ row }) => {
                const material = row.original;

                if (!material.file_url) {
                    return <span className="text-muted-foreground">-</span>;
                }

                return (
                    <a
                        href={material.file_url}
                        target="_blank"
                        rel="noreferrer"
                        className="inline-flex items-center gap-1 text-primary underline underline-offset-4 hover:text-primary/80"
                    >
                        <Paperclip className="h-3.5 w-3.5" />
                        {material.file_name}
                    </a>
                );
            },
        },
    ];

    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 material = row.original;

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

    return columns;
}
