import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { ImagePreviewButton } from '@/components/dialogs';
import { RowActions } from '@/components/data-display';

export type Expense = {
    id: number;
    amount: number;
    formatted_amount: string;
    description: string;
    receipt_key: string | null;
    receipt_url: string | null;
    receipt_conversion_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;
    can: (permission: string) => boolean;
};

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

    const columns: ColumnDef<Expense>[] = [
        {
            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]">
                    {row.getValue('description') as string}
                </span>
            ),
        },
        {
            id: 'receipt',
            header: () => <span>Bukti</span>,
            cell: ({ row }) => {
                const receiptConversionUrl = row.original.receipt_conversion_url;
                const receiptUrl = row.original.receipt_url;

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

                return (
                    <ImagePreviewButton
                        srcs={[receiptConversionUrl]}
                        modalSrc={receiptUrl ?? undefined}
                        title={row.original.description}
                    />
                );
            },
        },
        {
            accessorKey: 'formatted_amount',
            header: () => <span>Jumlah</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {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>;
            },
        },
    ];

    if (can('expenses.update') || can('expenses.delete')) {
        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 expense = row.original;

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

    return columns;
}
