import { useMemo } from "react";
import { MRT_ColumnDef } from "mantine-react-table";
import { ProfitTableDataItem } from "../../../../../../../client";
import { GroupStatisticsTable } from "../../ProfitTableSegmentedControl/ProfitTableSegmentedControl.tsx";
import { DealStatus, DealStatusDictionary } from "../../../../../../../shared/enums/DealStatus.ts";

type Props = {
    groupTableBy: GroupStatisticsTable;
}

export const useProfitTableColumns = ({ groupTableBy }: Props) => {
    const groupedValueHeader = {
        [GroupStatisticsTable.BY_DATES]: "Дата",
        [GroupStatisticsTable.BY_CLIENTS]: "Клиент",
        [GroupStatisticsTable.BY_STATUSES]: "Статус",
        [GroupStatisticsTable.BY_MARKETPLACES]: "Маркетплейс",
        [GroupStatisticsTable.BY_WAREHOUSES]: "Склад отгрузки",
    };

    return useMemo<MRT_ColumnDef<ProfitTableDataItem>[]>(
        () => [
            {
                accessorKey: "groupedValue",
                header: groupedValueHeader[groupTableBy],
                enableSorting: groupTableBy === GroupStatisticsTable.BY_DATES,
                Cell: ({ row }) => {
                    if (groupTableBy === GroupStatisticsTable.BY_STATUSES) {
                        const statusIndex = row.original.groupedValue as DealStatus;
                        return DealStatusDictionary[statusIndex];
                    }
                    return row.original.groupedValue;
                },
            },
            {
                accessorKey: "dealsCount",
                header: "Кол-во",
            },
            {
                accessorKey: "profit",
                header: "Прибыль",
                Cell: ({ row }) =>
                    row.original.profit.toLocaleString("ru-RU") + "₽",
            },
            {
                accessorKey: "revenue",
                header: "Выручка",
                Cell: ({ row }) =>
                    row.original.revenue.toLocaleString("ru-RU") + "₽",
            },
        ],
        [groupTableBy],
    );
};
