import type { UpdateUser, UpdateUserProfile } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type {
	ChangePasswordRequest,
	LoginRequest,
	RegisterRequest,
} from "@/api/user-api-client";
import { userApiClient } from "@/api/user-api-client";

// Query hooks
export function useUser(userId: string) {
	return useQuery({
		queryKey: ["user", userId],
		queryFn: () => userApiClient.getUser(userId),
		enabled: !!userId,
	});
}

export function useUserByEmail(email: string) {
	return useQuery({
		queryKey: ["user-by-email", email],
		queryFn: () => userApiClient.getUserByEmail(email),
		enabled: !!email,
	});
}

export function useUserByUsername(username: string) {
	return useQuery({
		queryKey: ["user-by-username", username],
		queryFn: () => userApiClient.getUserByUsername(username),
		enabled: !!username,
	});
}

export function useUsers(params?: {
	limit?: number;
	offset?: number;
	role?: string;
}) {
	return useQuery({
		queryKey: ["users", params],
		queryFn: () => userApiClient.getUsers(params),
	});
}

export function useCurrentUser() {
	return useQuery({
		queryKey: ["current-user"],
		queryFn: () => userApiClient.getCurrentUser(),
	});
}

export function useUserProfile(userId: string) {
	return useQuery({
		queryKey: ["user-profile", userId],
		queryFn: () => userApiClient.getUserProfile(userId),
		enabled: !!userId,
	});
}

export function useUserStats(userId: string) {
	return useQuery({
		queryKey: ["user-stats", userId],
		queryFn: () => userApiClient.getUserStats(userId),
		enabled: !!userId,
	});
}

// Mutation hooks
export function useLogin() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: (data: LoginRequest) => userApiClient.login(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["current-user"] });
		},
	});
}

export function useRegister() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: (data: RegisterRequest) => userApiClient.register(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["current-user"] });
		},
	});
}

export function useLogout() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: () => userApiClient.logout(),
		onSuccess: () => {
			queryClient.clear();
		},
	});
}

export function useUpdateUser() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: ({ userId, data }: { userId: string; data: UpdateUser }) =>
			userApiClient.updateUser(userId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["user", variables.userId] });
			queryClient.invalidateQueries({ queryKey: ["current-user"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (userId: string) => userApiClient.deleteUser(userId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["users"] });
		},
	});
}

export function useChangePassword() {
	return useMutation({
		mutationFn: (data: ChangePasswordRequest) =>
			userApiClient.changePassword(data),
	});
}

export function useUpdateUserProfile() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: ({
			userId,
			data,
		}: {
			userId: string;
			data: UpdateUserProfile;
		}) => userApiClient.updateUserProfile(userId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["user-profile", variables.userId],
			});
		},
	});
}

export function useForgotPassword() {
	return useMutation({
		mutationFn: (email: string) => userApiClient.forgotPassword(email),
	});
}

export function useResetPassword() {
	return useMutation({
		mutationFn: ({
			token,
			newPassword,
		}: {
			token: string;
			newPassword: string;
		}) => userApiClient.resetPassword(token, newPassword),
	});
}

export function useVerifyEmail() {
	return useMutation({
		mutationFn: (token: string) => userApiClient.verifyEmail(token),
	});
}

export function useResendVerificationEmail() {
	return useMutation({
		mutationFn: (email: string) => userApiClient.resendVerificationEmail(email),
	});
}
