"use client";

import { useBoardsContext } from "@/app/deals/contexts/BoardsContext";
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
import useStatusesList from "@/hooks/lists/useStatusesList";
import { StatusSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

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

const useStatusesContextState = (): StatusesContextState => {
    const { selectedBoard } = useBoardsContext();
    const {
        statuses,
        setStatuses,
        refetch: refetchStatuses,
        queryKey,
    } = useStatusesList({
        boardId: selectedBoard?.id,
    });

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

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

export const [StatusesContextProvider, useStatusesContext] =
    makeContext<StatusesContextState>(useStatusesContextState, "Statuses");
