"use client"

import { useQuery } from "@tanstack/react-query"
import { apiClient } from "@/lib/api/client"
import type {
  AdminAuditListResponse,
  AdminPublicSharesListResponse,
  AdminStatsResponse,
  AdminUserDetail,
  AdminUserGroupsListResponse,
  AdminUsersListResponse,
} from "@/lib/api/admin-types"
import { useAuthReady } from "@/lib/api/use-auth-ready"

export type AdminUsersQueryParams = {
  page?: number
  page_size?: number
  q?: string
  sort?: string
  status?: string
  role?: string
  group_id?: string
}

export type AdminUserGroupsQueryParams = {
  page?: number
  page_size?: number
  q?: string
}

export function useAdminStats() {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "stats"],
    queryFn: () => apiClient.get<AdminStatsResponse>("/admin/stats"),
    enabled: ready && authenticated,
  })
}

export function useAdminUsers(params: AdminUsersQueryParams = {}) {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "users", params],
    queryFn: () =>
      apiClient.get<AdminUsersListResponse>("/admin/users", {
        page: params.page?.toString(),
        page_size: params.page_size?.toString(),
        q: params.q,
        sort: params.sort,
        status: params.status,
        role: params.role,
        group_id: params.group_id,
      }),
    enabled: ready && authenticated,
  })
}

export function useAdminUserGroups(params: AdminUserGroupsQueryParams = {}) {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "user-groups", params],
    queryFn: () =>
      apiClient.get<AdminUserGroupsListResponse>("/admin/user-groups", {
        page: params.page?.toString(),
        page_size: params.page_size?.toString(),
        q: params.q,
      }),
    enabled: ready && authenticated,
  })
}

export function useAdminUser(userId: string | null) {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "users", userId],
    queryFn: () => apiClient.get<AdminUserDetail>(`/admin/users/${userId}`),
    enabled: ready && authenticated && Boolean(userId),
  })
}

export function useAdminPublicShares(
  params: { page?: number; page_size?: number; q?: string; sort?: string } = {}
) {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "public-shares", params],
    queryFn: () =>
      apiClient.get<AdminPublicSharesListResponse>("/admin/public-shares", {
        page: params.page?.toString(),
        page_size: params.page_size?.toString(),
        q: params.q,
        sort: params.sort,
      }),
    enabled: ready && authenticated,
  })
}

export function useAdminAuditLogs(params: { page?: number; page_size?: number } = {}) {
  const { ready, authenticated } = useAuthReady()
  return useQuery({
    queryKey: ["admin", "audit", params],
    queryFn: () =>
      apiClient.get<AdminAuditListResponse>("/admin/audit", {
        page: params.page?.toString(),
        page_size: params.page_size?.toString(),
      }),
    enabled: ready && authenticated,
  })
}
