"use client";

import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
import useStatusesList from "@/hooks/lists/useStatusesList";
import { BoardSchema, StatusSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type BoardStatusesContextState = {
    board: BoardSchema;
    statuses: StatusSchema[];
    setStatuses: (statuses: StatusSchema[]) => void;
    refetchStatuses: () => void;
    statusesCrud: StatusesCrud;
};

type Props = {
    board: BoardSchema;
};

const useBoardStatusesContextState = ({
    board,
}: Props): BoardStatusesContextState => {
    const {
        statuses,
        setStatuses,
        refetch: refetchStatuses,
        queryKey,
    } = useStatusesList({
        boardId: board.id,
    });

    const statusesCrud = useStatusesCrud({
        statuses,
        boardId: board.id,
        queryKey,
    });

    return {
        board,
        statuses,
        setStatuses,
        refetchStatuses,
        statusesCrud,
    };
};

export const [StatusesMobileContextProvider, useStatusesMobileContext] =
    makeContext<BoardStatusesContextState, Props>(
        useBoardStatusesContextState,
        "BoardStatuses"
    );
