import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ProjectSchema } from "@/lib/client";
import {
    getProjectsOptions,
    getProjectsQueryKey,
} from "@/lib/client/@tanstack/react-query.gen";

const useProjectsList = () => {
    const queryClient = useQueryClient();
    const { data, refetch } = useQuery({
        ...getProjectsOptions(),
    });

    const queryKey = getProjectsQueryKey();

    const setProjects = (statuses: ProjectSchema[]) => {
        queryClient.setQueryData(
            queryKey,
            (old: { items: ProjectSchema[] }) => ({
                ...old,
                items: statuses,
            })
        );
    };

    return {
        projects: data?.items ?? [],
        setProjects,
        refetch,
        queryKey,
    };
};

export default useProjectsList;
