import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { ImagePreviewButton } from '@/components/image-preview-button';
import { RowActions } from '@/components/row-actions';

export type Expense = {
    id: number;
    amount: number;
    formatted_amount: string;
    description: string;
    receipt_key: string | null;
    receipt_url: string | null;
    created_at: string;
    formatted_created_at: string;
    created_by: {
        user_profile: {
            full_name: string;
        };
    };
};

type CreateColumnsParams = {
    handleEdit: (expense: Expense) => void;
    handleDeleteClick: (expense: Expense) => void;
};

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

    return [
        {
            accessorKey: 'formatted_created_at',
            header: () => <span>Tanggal</span>,
            cell: ({ row }) => (
                <span>{row.getValue('formatted_created_at') as string}</span>
            ),
        },
        {
            accessorKey: 'description',
            header: () => <span>Keterangan</span>,
            cell: ({ row }) => (
                <span className="block max-w-[200px] truncate">
                    {row.getValue('description') as string}
                </span>
            ),
        },
        {
            id: 'receipt',
            header: () => <span>Bukti</span>,
            cell: ({ row }) => {
                const receiptUrl = row.original.receipt_url;

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

                return (
                    <ImagePreviewButton
                        srcs={[receiptUrl]}
                        title={row.original.description}
                    />
                );
            },
        },
        {
            accessorKey: 'formatted_amount',
            header: () => <span>Jumlah</span>,
            cell: ({ row }) => (
                <span className="font-medium text-red-600">
                    - {row.getValue('formatted_amount') as string}
                </span>
            ),
        },
        {
            id: 'created_by',
            header: () => <span>Oleh</span>,
            cell: ({ row }) => {
                const createdBy = row.original.created_by;

                return <span>{createdBy?.user_profile?.full_name ?? '-'}</span>;
            },
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const expense = row.original;

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