"use client";

import React from "react";
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: React.Dispatch<React.SetStateAction<BoardSchema[]>>;
    project: ProjectSchema;
    refetchBoards: () => void;
    boardsCrud: BoardsCrud;
};

type Props = {
    project: ProjectSchema;
};

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

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

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

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