import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { activityService } from "@/services/activity.service";
import type { PaginationParams } from "@/types";
import type { ActivityCreateRequest, ActivityType } from "@/types/activity";

export function useActivities(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.activities.list(params),
    queryFn: () => activityService.listActivities(params),
  });
}

export function useActivityTypes(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.activityTypes.list(params),
    queryFn: () => activityService.listActivityTypes(params),
  });
}

export function useCreateActivity() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: ActivityCreateRequest) => activityService.createActivity(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.activities.all }),
  });
}

export function useUpdateActivity() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<ActivityCreateRequest> }) =>
      activityService.updateActivity(id, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.activities.all }),
  });
}

export function useDeleteActivity() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => activityService.deleteActivity(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.activities.all }),
  });
}

export function useCreateActivityType() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: Pick<ActivityType, "name">) => activityService.createActivityType(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.activityTypes.all }),
  });
}

export function useDeleteActivityType() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => activityService.deleteActivityType(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.activityTypes.all }),
  });
}
