import {useMemo} from "react";
import {MRT_ColumnDef} from "mantine-react-table";
import {ServiceSchema} from "../../../../client";
import {List} from "@mantine/core";

export const useServicesTableColumns = () => {
    const getPriceRow = (service: ServiceSchema) => {
        if (service.priceRanges.length == 0)
            return <>{service.price}₽</>;
        return (
            <>
                <List>
                    {service.priceRanges.map(range => (
                        <List.Item>
                            {`${range.fromQuantity} - ${range.toQuantity}: ${range.price}₽`}
                        </List.Item>
                    ))}
                </List>
            </>
        );
    }

    return useMemo<MRT_ColumnDef<ServiceSchema>[]>(() => [
        {
            accessorKey: "category",
            header: "Категория",
            enableGrouping: false,
            enableSorting: false,
            accessorFn: (row) => row.category.name
        },
        {
            accessorKey: "name",
            header: "Услуга",
            enableGrouping: false,
            enableSorting: false,

        },
        {
            accessorKey: "price",
            header: "Цена",
            enableGrouping: false,
            enableSorting: false,
            Cell: ({row}) => getPriceRow(row.original)
        },
        {
            accessorKey: "cost",
            header: "Себестоимость",
            enableGrouping: false,
            enableSorting: false,
        },

    ], []);

}