"use client";

import useModulesWithAttrsList from "@/app/modules/hooks/useModulesWithAttrsList";
import { ModuleWithAttributesSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";
import useModulesActions, { ModulesActions } from "@/app/modules/hooks/useModulesActions";

type ModulesContextState = {
    modules: ModuleWithAttributesSchema[];
    refetchModules: () => void;
    modulesActions: ModulesActions;
};

const useModulesContextState = (): ModulesContextState => {
    const { modules, refetch } = useModulesWithAttrsList();
    const modulesActions = useModulesActions({ refetchModules: refetch });

    return {
        modules,
        refetchModules: refetch,
        modulesActions,
    };
};

export const [ModulesContextProvider, useModulesContext] =
    makeContext<ModulesContextState>(useModulesContextState, "Modules");
