import { RowActions } from '@/components/row-actions';
import { Badge } from '@/components/ui/badge';
import type { AcademicTerm } from '@/types/academic-term';
import { SemesterLabels, type Semester } from '@/types/academic-term';
import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';

export type { AcademicTerm } from '@/types/academic-term';

type CreateColumnsParams = {
    handleEdit: (academicTerm: AcademicTerm) => void;
    handleDeleteClick: (academicTerm: AcademicTerm) => void;
};

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

    return [
        {
            accessorKey: 'name',
            header: () => <span>Nama</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('name') as string}
                </span>
            ),
        },
        {
            accessorKey: 'semester',
            header: () => <span>Semester</span>,
            cell: ({ row }) => {
                const semester = row.getValue('semester') as Semester;
                return <span>{SemesterLabels[semester]}</span>;
            },
        },
        {
            accessorKey: 'formatted_start_date',
            header: () => <span>Tanggal Mulai</span>,
            cell: ({ row }) => (
                <span>{row.getValue('formatted_start_date') as string | null}</span>
            ),
        },
        {
            accessorKey: 'formatted_end_date',
            header: () => <span>Tanggal Selesai</span>,
            cell: ({ row }) => (
                <span>{row.getValue('formatted_end_date') as string | null}</span>
            ),
        },
        {
            accessorKey: 'is_active',
            header: () => <span className="block text-center">Status</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const isActive = row.getValue('is_active') as boolean;
                return (
                    <div className="flex justify-center">
                        <Badge variant={isActive ? 'default' : 'secondary'}>
                            {isActive ? 'Aktif' : 'Nonaktif'}
                        </Badge>
                    </div>
                );
            },
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const academicTerm = row.original;

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