"use client"

import { useMutation, useQueryClient } from "@tanstack/react-query"
import { apiClient } from "@/lib/api/client"
import type {
  AdminBulkUsersRequest,
  AdminBulkUsersResponse,
  AdminCreateUserGroupRequest,
  AdminCreateUserRequest,
  AdminInviteUserRequest,
  AdminSetQuotaRequest,
  AdminSetUserGroupMembersRequest,
  AdminSetUserRoleRequest,
  AdminUpdateUserGroupRequest,
  AdminUpdateUserRequest,
  AdminUser,
  AdminUserDetail,
  AdminUserGroup,
} from "@/lib/api/admin-types"

export function useCreateAdminUser() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminCreateUserRequest) =>
      apiClient.post<AdminUser>("/admin/users", body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useInviteAdminUser() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminInviteUserRequest) =>
      apiClient.post<AdminUser>("/admin/users/invite", body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useUpdateAdminUser(userId: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminUpdateUserRequest) =>
      apiClient.put<AdminUserDetail>(`/admin/users/${userId}`, body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "users", userId] })
    },
  })
}

export function useSetAdminUserRole(userId: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminSetUserRoleRequest) =>
      apiClient.put<AdminUserDetail>(`/admin/users/${userId}/role`, body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "users", userId] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useSetAdminUserQuota(userId: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminSetQuotaRequest) =>
      apiClient.put<void>(`/admin/users/${userId}/quota`, body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users", userId] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useDisableAdminUser() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (userId: string) =>
      apiClient.post<void>(`/admin/users/${userId}/disable`),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useReactivateAdminUser() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (userId: string) =>
      apiClient.post<void>(`/admin/users/${userId}/reactivate`),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useRevokeAdminPublicShare() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: ({
      shareId,
      ownerNcUserId,
    }: {
      shareId: string
      ownerNcUserId: string
    }) =>
      apiClient.delete(
        `/admin/public-shares/${shareId}?owner_nc_user_id=${encodeURIComponent(ownerNcUserId)}`
      ),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "public-shares"] })
    },
  })
}

export function useDeleteAdminUser() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (userId: string) => apiClient.delete(`/admin/users/${userId}`),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}

export function useCreateAdminUserGroup() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminCreateUserGroupRequest) =>
      apiClient.post<AdminUserGroup>("/admin/user-groups", body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "user-groups"] })
    },
  })
}

export function useUpdateAdminUserGroup(groupId: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminUpdateUserGroupRequest) =>
      apiClient.put<AdminUserGroup>(`/admin/user-groups/${groupId}`, body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "user-groups"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
    },
  })
}

export function useDeleteAdminUserGroup() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (groupId: string) => apiClient.delete(`/admin/user-groups/${groupId}`),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "user-groups"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
    },
  })
}

export function useSetAdminUserGroupMembers(groupId: string) {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminSetUserGroupMembersRequest) =>
      apiClient.put<AdminUserGroup>(`/admin/user-groups/${groupId}/members`, body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "user-groups"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
    },
  })
}

export function useBulkAdminUsers() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (body: AdminBulkUsersRequest) =>
      apiClient.post<AdminBulkUsersResponse>("/admin/users/bulk", body),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["admin", "users"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "user-groups"] })
      void queryClient.invalidateQueries({ queryKey: ["admin", "stats"] })
    },
  })
}
