import type { ColumnDef } from '@tanstack/react-table';
import { format } from 'date-fns';
import { Pencil, Trash2 } from 'lucide-react';
import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
import type { Announcement } from '@/types/announcement';

export type { Announcement } from '@/types/announcement';

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

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

    return [
        {
            accessorKey: 'title',
            header: () => <span>Judul</span>,
            cell: ({ row }) => {
                const announcement = row.original;

                return (
                    <div>
                        <p className="font-medium">{announcement.title}</p>
                        <p className="line-clamp-1 max-w-md text-xs text-muted-foreground">
                            {announcement.content}
                        </p>
                    </div>
                );
            },
        },
        {
            accessorKey: 'department.name',
            header: () => <span>Target</span>,
            cell: ({ row }) => {
                const announcement = row.original;

                if (!announcement.department && !announcement.enrollment_year) {
                    return <Badge variant="outline">Semua Mahasiswa</Badge>;
                }

                return (
                    <div className="flex flex-wrap gap-1">
                        <Badge variant="secondary">
                            {announcement.department?.name ?? 'Semua Jurusan'}
                        </Badge>
                        {announcement.enrollment_year && (
                            <Badge variant="secondary">
                                Angkatan {announcement.enrollment_year}
                            </Badge>
                        )}
                    </div>
                );
            },
        },
        {
            accessorKey: 'creator.profile.full_name',
            header: () => <span>Dibuat Oleh</span>,
            cell: ({ row }) => row.original.creator?.profile?.full_name ?? '-',
        },
        {
            accessorKey: 'created_at',
            header: () => <span>Tanggal</span>,
            cell: ({ row }) =>
                format(new Date(row.original.created_at), 'd MMM yyyy, HH:mm'),
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => (
                <RowActions
                    actions={[
                        {
                            label: 'Edit',
                            icon: <Pencil className="h-4 w-4" />,
                            show: canUpdate,
                            onClick: () => handleEdit(row.original),
                        },
                        {
                            label: 'Hapus',
                            icon: (
                                <Trash2 className="h-4 w-4 text-destructive" />
                            ),
                            show: canDelete,
                            onClick: () => handleDeleteClick(row.original),
                        },
                    ]}
                />
            ),
        },
    ];
}
