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";
import { formatDate } from "../../../../../../../types/utils.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]: "Склад отгрузки",
        [GroupStatisticsTable.BY_MANAGERS]: "Менеджер",
    };

    const getConditionalColumns = (): MRT_ColumnDef<ProfitTableDataItem>[] => {
        if (groupTableBy === GroupStatisticsTable.BY_DATES) {
            return [
                {
                    accessorKey: "revenue",
                    header: "Выручка",
                    Cell: ({ row }) =>
                        row.original.revenue.toLocaleString("ru-RU") + "₽",
                    size: 50,
                },
                {
                    accessorKey: "expenses",
                    header: "Расходы",
                    Cell: ({ row }) =>
                        row.original.expenses?.toLocaleString("ru-RU") + "₽",
                    size: 50,
                }
            ] as MRT_ColumnDef<ProfitTableDataItem>[];
        }
        return [];
    };

    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];
                    }
                    if (groupTableBy == GroupStatisticsTable.BY_DATES) {
                        return formatDate(row.original.groupedValue as string);
                    }
                    return row.original.groupedValue;
                },
                size: groupTableBy === GroupStatisticsTable.BY_DATES ? 40 : 80,
            },
            {
                accessorKey: "dealsCount",
                header: "Кол-во",
                size: 40,
            },
            ...getConditionalColumns(),
            {
                accessorKey: "profit",
                header: "Прибыль",
                Cell: ({ row }) =>
                    row.original.profit.toLocaleString("ru-RU") + "₽",
                size: 50,
            },
        ],
        [groupTableBy],
    );
};
