import type {
  CreateAuthor,
  CreateTimelineEvent,
  UpdateAuthor,
  UpdateTimelineEvent,
} from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { AuthorSearchParams } from "@/lib/api/author-api-client";
import { authorApiClient } from "@/lib/api/author-api-client";

// Query hooks
export function useAuthor(authorId: string) {
  return useQuery({
    queryKey: ["author", authorId],
    queryFn: () => authorApiClient.getAuthor(authorId),
    enabled: !!authorId,
  });
}

export function useAuthorBySlug(slug: string) {
  return useQuery({
    queryKey: ["author-by-slug", slug],
    queryFn: () => authorApiClient.getAuthorBySlug(slug),
    enabled: !!slug,
  });
}

export function useAuthors(params?: AuthorSearchParams) {
  return useQuery({
    queryKey: ["authors", params],
    queryFn: () => authorApiClient.getAuthors(params),
  });
}

export function useAuthorWorks(authorId: string) {
  return useQuery({
    queryKey: ["author-works", authorId],
    queryFn: () => authorApiClient.getAuthorWorks(authorId),
    enabled: !!authorId,
  });
}

export function useAuthorTimeline(authorId: string) {
  return useQuery({
    queryKey: ["author-timeline", authorId],
    queryFn: () => authorApiClient.getAuthorTimeline(authorId),
    enabled: !!authorId,
  });
}

export function useSearchAuthors(query: string) {
  return useQuery({
    queryKey: ["search-authors", query],
    queryFn: () => authorApiClient.searchAuthors(query),
    enabled: !!query && query.length > 2,
  });
}

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

  return useMutation({
    mutationFn: (data: CreateAuthor) => authorApiClient.createAuthor(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["authors"] });
    },
  });
}

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

  return useMutation({
    mutationFn: ({
      authorId,
      data,
    }: {
      authorId: string;
      data: UpdateAuthor;
    }) => authorApiClient.updateAuthor(authorId, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["author", variables.authorId],
      });
      queryClient.invalidateQueries({ queryKey: ["authors"] });
    },
  });
}

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

  return useMutation({
    mutationFn: (authorId: string) => authorApiClient.deleteAuthor(authorId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["authors"] });
    },
  });
}

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

  return useMutation({
    mutationFn: ({
      authorId,
      data,
    }: {
      authorId: string;
      data: CreateTimelineEvent;
    }) => authorApiClient.createTimelineEvent(authorId, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["author-timeline", variables.authorId],
      });
    },
  });
}

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

  return useMutation({
    mutationFn: ({
      eventId,
      data,
    }: {
      eventId: string;
      data: UpdateTimelineEvent;
    }) => authorApiClient.updateTimelineEvent(eventId, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["author-timeline"] });
    },
  });
}

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

  return useMutation({
    mutationFn: (eventId: string) =>
      authorApiClient.deleteTimelineEvent(eventId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["author-timeline"] });
    },
  });
}
