import type { Chat } from "../types/chat"
import camelcaseKeys from "camelcase-keys"
import { useInfiniteQuery, useQuery, type InfiniteData } from "@tanstack/react-query"
import { apiFetch } from "@/api"


interface ListChatsResponse {
  data: {
    chats: Array<{
      id: number,
      uid: string,
      label?: string,
      user_uid?: string,
      graph_uid?: string,
      created_at?: string,
      updated_at?: string,
      deleted_at?: string
    }>
  }
}


/**
 * Fetch the list of chats.
 */
export async function listChats(
  offset: number = 0,
  limit: number = 100,
  graphUid: string | "none" | "any" | null = "none"
): Promise<Chat[]> {
  if (graphUid === undefined) {
    graphUid = "none"
  }

  const res = await apiFetch<ListChatsResponse>({
    path: `/chats`,
    method: "GET",
    params: { offset, limit, graph_uid: graphUid },
  })
  return res.data.chats.map((chat) => camelcaseKeys(chat, { deep: true })) as Chat[]
}


/**
 * Custom hook to fetch the list of chats for a user.
 *
 * @param userId - The ID of the user whose chats are to be fetched.
 *
 * @returns A query object containing the list of chats.
 */
export const useListChats = ({
  userId,
  offset = 0,
  limit = 100,
  graphUid = "none"
}: {
  userId: string,
  offset?: number,
  limit?: number,
  graphUid?: string | "none" | "any" | null
}) => {
  return useQuery<Chat[]>({
    queryKey: ["listChats", userId, offset, limit, graphUid],
    queryFn: () => listChats(offset, limit, graphUid),
    enabled: !!userId && offset >= 0 && limit > 0,
    staleTime: 1000 * 60 * 5 // 5 minutes
  })
}

/**
 * Infinite chat list hook for incremental fetching (e.g., chat history sidebar).
 */
export const useInfiniteChats = ({
  userId,
  graphUid = "none",
  pageSize = 20
}: {
  userId: string,
  graphUid?: string | "none" | "any" | null,
  pageSize?: number
}) => {
  const normalizedGraphUid = graphUid ?? "none"
  return useInfiniteQuery<Chat[], Error, InfiniteData<Chat[]>, [string, string, string, string, number], number>({
    queryKey: ["listChats", userId, "infinite", normalizedGraphUid, pageSize],
    initialPageParam: 0,
    queryFn: ({ pageParam = 0 }) => listChats(pageParam, pageSize, normalizedGraphUid),
    getNextPageParam: (lastPage, allPages) => (
      lastPage.length === pageSize ? allPages.length * pageSize : undefined
    ),
    enabled: !!userId,
    staleTime: 1000 * 60 * 5,
  })
}
