import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { notificationService } from "@/services/notification.service";
import type { NotificationPreferences, NotificationRuleCreateRequest } from "@/types/notification";
import type { PaginationParams } from "@/types";

export function useNotifications(params?: PaginationParams & { type?: string; is_read?: boolean }) {
  return useQuery({
    queryKey: queryKeys.notifications(params),
    queryFn: () => notificationService.listNotifications(params),
  });
}

export function useUnreadNotificationCount() {
  return useQuery({
    queryKey: queryKeys.unreadNotifications,
    queryFn: () => notificationService.getUnreadNotificationCount(),
  });
}

export function useNotificationRules() {
  return useQuery({
    queryKey: queryKeys.notificationRules,
    queryFn: () => notificationService.listNotificationRules(),
  });
}

export function useNotificationPreferences() {
  return useQuery({
    queryKey: queryKeys.notificationPreferences,
    queryFn: () => notificationService.getNotificationPreferences(),
  });
}

export function useMarkNotificationRead() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => notificationService.markNotificationRead(id),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["notifications"] });
      qc.invalidateQueries({ queryKey: queryKeys.unreadNotifications });
    },
  });
}

export function useMarkAllNotificationsRead() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: () => notificationService.markAllNotificationsRead(),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ["notifications"] });
      qc.invalidateQueries({ queryKey: queryKeys.unreadNotifications });
    },
  });
}

export function useCreateNotificationRule() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: NotificationRuleCreateRequest) =>
      notificationService.createNotificationRule(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.notificationRules });
    },
  });
}

export function useUpdateNotificationRule() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      data,
    }: {
      id: number;
      data: Partial<NotificationRuleCreateRequest>;
    }) => notificationService.updateNotificationRule(id, data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.notificationRules });
    },
  });
}

export function useDeleteNotificationRule() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => notificationService.deleteNotificationRule(id),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.notificationRules });
    },
  });
}

export function useUpdateNotificationPreferences() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: Partial<NotificationPreferences>) =>
      notificationService.updateNotificationPreferences(data),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: queryKeys.notificationPreferences });
    },
  });
}
