import { useMemo } from "react";
import { MRT_Cell, MRT_ColumnDef, MRT_Row } from "mantine-react-table";
import { getDayOfWeek } from "../../../../../shared/lib/date.ts";
import { Box, Flex, Text } from "@mantine/core";
import { WorkShiftPlanningRow } from "../types/WorkShiftPlanningRow.tsx";
import { useWorkShiftsPlanningContext } from "../contexts/WorkShiftsPlanningContext.tsx";
import { PositionSchema } from "../../../../../client";
import PositionsMultiSelect from "../components/PositionsMultiSelect/PositionsMultiSelect.tsx";
import { dateToString } from "../../../../../types/utils.ts";


const useWorkShiftsPlanningColumns = () => {
    const { range } = useWorkShiftsPlanningContext();

    return useMemo<MRT_ColumnDef<WorkShiftPlanningRow>[]>(
        () => [
            {
                accessorKey: "name",
                header: "ФИО",
                Cell: ({ row }: { row: MRT_Row<WorkShiftPlanningRow> }) => (
                    <Flex direction={"column"}>
                        <Text size={"sm"}>
                            {row.original.user.firstName} {row.original.user.secondName}
                        </Text>
                    </Flex>
                ),
            },

            ...range.map(date => ({
                size: 100,
                accessorKey: dateToString(date) ?? "",
                header: dateToString(date) ?? "",
                enableSorting: false,
                enableColumnActions: false,
                Header: (
                    <Flex direction={"column"}>
                        <Box>{date.toLocaleString("ru").substring(0, 10)}</Box>
                        <Box>{getDayOfWeek(date.getDay())}</Box>
                    </Flex>
                ),
                Cell: ({ cell }: { cell: MRT_Cell<WorkShiftPlanningRow> }) => {
                    return (
                        <Flex direction={"column"}>
                            <PositionsMultiSelect
                                date={date}
                                positions={cell.getValue() as PositionSchema[]}
                                user={cell.row.original.user}
                            />
                        </Flex>
                    );
                },
            })),
        ],
        [range],
    );
};

export default useWorkShiftsPlanningColumns;
