'use client'
import { useSafeInfiniteQuery, useSafeQuery } from "@/hooks/use-safe-query";
import { getSurveyByIdAction, getSurveysAction, getSurveysForUserAction } from "../actions/surveys-actions";


// Hook for all survesys 
export function useSurveysQuery(params = {}) {
    return useSafeQuery(['surveys',params], () =>  getSurveysAction(params))
}

export function useAllSurveysInfiniteQuery(search: string = '') {
    return useSafeInfiniteQuery(
        ['surveys', search],
        // pageParam + 1 para evitar duplicación de datos
        ({ pageParam = 0 }) => getSurveysForUserAction({ page: pageParam + 1, limit: 10, search }),
        (lastPage, allPages) => {
            // Esta lógica determina el 'pageParam' para la siguiente página
            const nextPage = allPages.length;
            // Puedes añadir una condición para saber si hay más páginas
            if (lastPage.data.length < 10) return undefined;
            return nextPage;
        }
    )
}

export function useSurveysForUserQuery(params = {}) {
    return useSafeQuery(['surveys',params], () =>  getSurveysForUserAction(params))
}

export function useSurveysByIdQuery(id: number) {   
    return useSafeQuery(['surveys',id], () =>  getSurveyByIdAction(id))
}