"use client";

import { BoardsCrud, useBoardsCrud } from "@/hooks/cruds/useBoardsCrud";
import useBoardsList from "@/hooks/lists/useBoardsList";
import { BoardSchema, ProjectSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type ProjectBoardsContextState = {
    boards: BoardSchema[];
    setBoards: (boards: BoardSchema[]) => void;
    project: ProjectSchema;
    refetchBoards: () => void;
    boardsCrud: BoardsCrud;
};

type Props = {
    project: ProjectSchema;
};

const useProjectBoardsContextState = ({ project }: Props) => {
    const {
        boards,
        setBoards,
        refetch: refetchBoards,
        queryKey,
    } = useBoardsList({ projectId: project?.id });

    const boardsCrud = useBoardsCrud({
        boards,
        queryKey,
        projectId: project?.id,
    });

    return {
        boards,
        setBoards,
        project,
        refetchBoards,
        boardsCrud,
    };
};

export const [ProjectBoardsContextProvider, useProjectBoardsContext] =
    makeContext<ProjectBoardsContextState, Props>(
        useProjectBoardsContextState,
        "ProjectBoards"
    );
