import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
    Tooltip,
    TooltipContent,
    TooltipProvider,
    TooltipTrigger,
} from '@/components/ui/tooltip';

export type Supplier = {
    id: number;
    name: string;
    phone_number: string | null;
    address: string | null;
};

type CreateColumnsParams = {
    handleEdit: (supplier: Supplier) => void;
    handleDeleteClick: (supplier: Supplier) => void;
};

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

    return [
        {
            accessorKey: 'name',
            header: () => <span>Nama</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('name') as string}
                </span>
            ),
        },
        {
            accessorKey: 'phone_number',
            header: () => <span>No. Telepon</span>,
            cell: ({ row }) => (
                <span>{(row.getValue('phone_number') as string) ?? '-'}</span>
            ),
        },
        {
            accessorKey: 'address',
            header: () => <span>Alamat</span>,
            cell: ({ row }) => (
                <span className="block max-w-[200px] truncate">
                    {(row.getValue('address') as string) ?? '-'}
                </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 supplier = row.original;

                return (
                    <TooltipProvider>
                        <div className="flex items-center justify-center gap-1">
                            <Tooltip>
                                <TooltipTrigger asChild>
                                    <Button
                                        variant="ghost"
                                        size="icon"
                                        onClick={() => handleEdit(supplier)}
                                    >
                                        <Pencil className="h-4 w-4" />
                                    </Button>
                                </TooltipTrigger>
                                <TooltipContent side="top">Edit</TooltipContent>
                            </Tooltip>

                            <Tooltip>
                                <TooltipTrigger asChild>
                                    <Button
                                        variant="ghost"
                                        size="icon"
                                        onClick={() =>
                                            handleDeleteClick(supplier)
                                        }
                                    >
                                        <Trash2 className="h-4 w-4 text-destructive" />
                                    </Button>
                                </TooltipTrigger>
                                <TooltipContent side="top">
                                    Hapus
                                </TooltipContent>
                            </Tooltip>
                        </div>
                    </TooltipProvider>
                );
            },
        },
    ];
}
