import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query' import type { Ref } from 'vue' import { apiClient } from '@/lib/axios' import type { CrowdList, CreateCrowdListDto, UpdateCrowdListDto } from '@/types/crowdList' import type { Person } from '@/types/person' interface ApiResponse { success: boolean data: T message?: string } interface PaginatedResponse { data: T[] links: Record meta: { current_page: number per_page: number total: number last_page: number } } export function useCrowdLists(orgId: Ref, eventId: Ref) { return useQuery({ queryKey: ['crowd-lists', eventId], queryFn: async () => { const { data } = await apiClient.get<{ data: CrowdList[] }>( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists`, ) return data.data }, enabled: () => !!orgId.value && !!eventId.value, }) } export function useCrowdListPersons(orgId: Ref, eventId: Ref, crowdListId: Ref) { return useQuery({ queryKey: ['crowd-lists', eventId, 'persons', crowdListId], queryFn: async () => { const { data } = await apiClient.get>( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${crowdListId.value}/persons`, ) return data }, enabled: () => !!eventId.value && !!crowdListId.value, }) } export function useCreateCrowdList(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async (payload: CreateCrowdListDto) => { const { data } = await apiClient.post>( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists`, payload, ) return data.data }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) }, }) } export function useUpdateCrowdList(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async ({ id, ...payload }: UpdateCrowdListDto & { id: string }) => { const { data } = await apiClient.put>( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${id}`, payload, ) return data.data }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) }, }) } export function useDeleteCrowdList(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async (id: string) => { await apiClient.delete(`/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${id}`) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) }, }) } export function useAddPersonToCrowdList(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async ({ listId, personId }: { listId: string; personId: string }) => { const { data } = await apiClient.post>( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${listId}/persons`, { person_id: personId }, ) return data.data }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) }, }) } export function useRemovePersonFromCrowdList(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async ({ listId, personId }: { listId: string; personId: string }) => { await apiClient.delete( `/organisations/${orgId.value}/events/${eventId.value}/crowd-lists/${listId}/persons/${personId}`, ) }, onSuccess: (_data, variables) => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value, 'persons', variables.listId], }) }, }) } export function useApproveAllPending(orgId: Ref, eventId: Ref) { const queryClient = useQueryClient() return useMutation({ mutationFn: async (personIds: string[]) => { const results = await Promise.allSettled( personIds.map(id => apiClient.post>( `/organisations/${orgId.value}/events/${eventId.value}/persons/${id}/approve`, ), ), ) const approved = results.filter(r => r.status === 'fulfilled').length return { approved, total: personIds.length } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['crowd-lists', eventId.value] }) queryClient.invalidateQueries({ queryKey: ['persons', eventId.value] }) }, }) }