import { useQuery } from "@tanstack/react-query";

import { client } from "@/lib/rpc";
import { InferResponseType } from "hono";
import { TaskStatus } from "../types";

interface UseGetEpicsProps {
  workspaceId: string;
  projectId?: string;
  assigneeId?: string;
  status?: TaskStatus;
  search?: string;
  dueDate?: string;
}

export type EpicsResponseType = InferResponseType<typeof client.api.epics["$get"], 200>;

export const useGetEpics = ({
  workspaceId,
  projectId,
  assigneeId,
  status,
  search,
  dueDate,
}: UseGetEpicsProps) => {
  const query = useQuery({
    queryKey: [
      "epics",
      workspaceId,
      projectId,
      assigneeId,
      status,
      search,
      dueDate,
    ],
    queryFn: async () => {
      const response = await client.api.epics["$get"]({
        query: {
          workspaceId,
          projectId: projectId || undefined,
          assigneeId: assigneeId || undefined,
          status: status || undefined,
          search: search || undefined,
          dueDate: dueDate || undefined,
        },
      });

      if (!response.ok) {
        throw new Error("Failed to fetch epics");
      }

      const { data } = await response.json();

      return data;
    },
  });

  return query;
}; 