import { useMemo } from "react";
import { IconEdit } from "@tabler/icons-react";
import { DataTableColumn } from "mantine-datatable";
import { ActionIcon, Tooltip } from "@mantine/core";
import { DealSchema } from "@/lib/client";
import { utcDateTimeToLocalString } from "@/utils/datetime";

type Props = {
    onEditClick: (deal: DealSchema) => void;
};

const useDealsTableColumns = ({ onEditClick }: Props) => {
    return useMemo(
        () =>
            [
                {
                    accessor: "actions",
                    title: "Действия",
                    sortable: false,
                    textAlign: "center",
                    width: "0%",
                    render: deal => (
                        <Tooltip label="Редактировать">
                            <ActionIcon
                                bdrs={"md"}
                                size={"lg"}
                                onClick={() => onEditClick(deal)}
                                variant={"default"}>
                                <IconEdit />
                            </ActionIcon>
                        </Tooltip>
                    ),
                },
                {
                    accessor: "id",
                    title: "Номер",
                    sortable: true,
                    width: "30%",
                },
                {
                    accessor: "name",
                    title: "Название",
                    width: "40%",
                },
                {
                    title: "Дата создания",
                    accessor: "createdAt",
                    render: deal => utcDateTimeToLocalString(deal.createdAt),
                    sortable: true,
                    width: "30%",
                },
            ] as DataTableColumn<DealSchema>[],
        [onEditClick]
    );
};

export default useDealsTableColumns;
