import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { adminService } from "./service";
import type {
  AdminPaginationParams,
  UpdateSettingDto,
  UpdateUserRoleDto,
  UpdateUserSubscriptionDto,
} from "./types";

export const AdminQueryKeys = {
  all: ["admin"] as const,
  analytics: () => [...AdminQueryKeys.all, "analytics"] as const,
  settings: () => [...AdminQueryKeys.all, "settings"] as const,
  usageLimits: (params?: AdminPaginationParams) =>
    [...AdminQueryKeys.all, "usageLimits", params] as const,
  users: (params?: AdminPaginationParams) =>
    [...AdminQueryKeys.all, "users", params] as const,
  user: (id: string) => [...AdminQueryKeys.all, "user", id] as const,
};

// Analytics
export const useAdminAnalytics = (enabled = true) => {
  return useQuery({
    queryKey: AdminQueryKeys.analytics(),
    queryFn: () => adminService.getAnalyticsOverview(),
    enabled,
  });
};

// Settings
export const useAdminSettings = () => {
  return useQuery({
    queryKey: AdminQueryKeys.settings(),
    queryFn: () => adminService.getAllSettings(),
  });
};

export const useUpdateSetting = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ key, dto }: { key: string; dto: UpdateSettingDto }) =>
      adminService.updateSetting(key, dto),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.settings() });
    },
  });
};

// Usage Limits
export const useAdminUsageLimits = (params?: AdminPaginationParams) => {
  return useQuery({
    queryKey: AdminQueryKeys.usageLimits(params),
    queryFn: () => adminService.getAllUsageLimits(params),
  });
};

export const useResetAllUsageLimits = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: () => adminService.resetAllUsageLimits(),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.usageLimits() });
    },
  });
};

// Users
export const useAdminUsers = (
  params?: AdminPaginationParams,
  enabled = true,
) => {
  return useQuery({
    queryKey: AdminQueryKeys.users(params),
    queryFn: () => adminService.getAllUsers(params),
    enabled,
  });
};

export const useAdminUserById = (id: string) => {
  return useQuery({
    queryKey: AdminQueryKeys.user(id),
    queryFn: () => adminService.getUserById(id),
    enabled: !!id,
  });
};

export const useDeleteUser = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: string) => adminService.deleteUser(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.users() });
    },
  });
};

export const useUpdateUserRole = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, dto }: { id: string; dto: UpdateUserRoleDto }) =>
      adminService.updateUserRole(id, dto),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.users() });
    },
  });
};

export const useUpdateUserSubscription = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, dto }: { id: string; dto: UpdateUserSubscriptionDto }) =>
      adminService.updateUserSubscription(id, dto),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.users() });
    },
  });
};

export const useToggleUserActive = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: string) => adminService.toggleUserActive(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: AdminQueryKeys.users() });
    },
  });
};
