import { useMemo } from "react";
import { IconEdit } from "@tabler/icons-react";
import { DataTableColumn } from "mantine-datatable";
import ActionIconWithTip from "@/components/ui/ActionIconWithTip/ActionIconWithTip";
import useIsMobile from "@/hooks/utils/useIsMobile";
import { DealSchema } from "@/lib/client";
import { ModuleNames } from "@/modules/modules";
import { utcDateTimeToLocalString } from "@/utils/datetime";

type Props = {
    onEditClick: (deal: DealSchema) => void;
    modulesSet: Set<ModuleNames>;
};

const useDealsTableColumns = ({ onEditClick, modulesSet }: Props) => {
    const isMobile = useIsMobile();

    return useMemo(
        () =>
            [
                {
                    accessor: "actions",
                    title: isMobile ? "" : "Действия",
                    sortable: false,
                    textAlign: "center",
                    width: "0%",
                    render: deal => (
                        <ActionIconWithTip
                            tipLabel={"Редактировать"}
                            onClick={() => onEditClick(deal)}
                            variant={isMobile ? "subtle" : "default"}>
                            <IconEdit />
                        </ActionIconWithTip>
                    ),
                },
                {
                    accessor: "id",
                    title: isMobile ? "№" : "Номер",
                    sortable: true,
                },
                {
                    accessor: "name",
                    title: "Название",
                },
                {
                    title: "Дата создания",
                    accessor: "createdAt",
                    render: deal => utcDateTimeToLocalString(deal.createdAt),
                    sortable: true,
                },
                {
                    title: "Клиент",
                    accessor: "client.name",
                    hidden: !modulesSet.has(ModuleNames.CLIENTS),
                },
                {
                    title: "Общая стоимость",
                    accessor: "totalPrice",
                    render: deal =>
                        deal.totalPrice
                            ? `${deal.totalPrice.toLocaleString("ru")}₽`
                            : "0₽",
                    hidden: !modulesSet.has(ModuleNames.FULFILLMENT_BASE),
                },
            ] as DataTableColumn<DealSchema>[],
        [onEditClick]
    );
};

export default useDealsTableColumns;
