import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";

import { api } from "../client";
import type { components } from "../schema";
import { HttpError, FieldRejection, VisibilityError } from "../errors";
import { keys, type ObjectListParams } from "../query-keys";

type ObjectCreateRequest = components["schemas"]["ObjectCreateRequest"];
type ObjectUpdateRequest = components["schemas"]["ObjectUpdateRequest"];
type Visibility = "draft" | "internal" | "public";

export function useObjectsPage(params: ObjectListParams) {
  return useQuery({
    queryKey: keys.objectsPage(params),
    placeholderData: keepPreviousData,
    queryFn: async () => {
      const { data, error } = await api.GET("/api/admin/objects", {
        params: {
          query: {
            limit: params.limit,
            offset: params.offset,
            sort: params.sort,
            order: params.order,
            visibility: params.visibility,
            q: params.q,
          },
        },
      });

      if (error || !data) throw new Error("failed to load objects");

      return data;
    },
  });
}

export function useObject(id: string) {
  return useQuery({
    queryKey: keys.object(id),
    queryFn: async () => {
      const { data, response } = await api.GET("/api/admin/objects/{id}", {
        params: { path: { id } },
      });

      if (response.status === 404) return null;

      if (!data) throw new Error("failed to load object");

      return data;
    },
    // A 404 resolves to null rather than erroring, so don't retry it.
    retry: false,
  });
}

export function useCreateObject() {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: async (body: ObjectCreateRequest) => {
      const { data, error, response } = await api.POST("/api/admin/objects", { body });

      if (error || !data) throw new HttpError(response.status);

      return data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: keys.objects() }),
    meta: { successMessage: "toast.created", suppressErrorToast: true },
  });
}

export function useUpdateObject() {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: async ({ id, body }: { id: string; body: ObjectUpdateRequest }) => {
      const { response } = await api.PUT("/api/admin/objects/{id}", {
        params: { path: { id } },
        body,
      });

      if (response.status !== 204) throw new HttpError(response.status);
    },
    onSuccess: (_d, { id }) => {
      void qc.invalidateQueries({ queryKey: keys.objects() });
      void qc.invalidateQueries({ queryKey: keys.object(id) });
      void qc.invalidateQueries({ queryKey: keys.search() });
    },
    meta: { successMessage: "toast.saved", suppressErrorToast: true },
  });
}

export function useSetFields() {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: async ({ id, fields }: { id: string; fields: Record<string, unknown> }) => {
      const { response, error } = await api.PUT("/api/admin/objects/{id}/fields", {
        params: { path: { id } },
        body: fields as Record<string, never>,
      });

      if (response.status === 204) return;

      if (response.status === 422 && error && typeof error === "object" && "field" in error) {
        const detail = error as { field: string; code: string };
        throw new FieldRejection(detail.field, detail.code);
      }

      throw new HttpError(response.status);
    },
    onSuccess: (_d, { id }) => {
      void qc.invalidateQueries({ queryKey: keys.object(id) });
    },
    meta: { suppressErrorToast: true },
  });
}

export function useDeleteObject() {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: async (id: string) => {
      const { response } = await api.DELETE("/api/admin/objects/{id}", {
        params: { path: { id } },
      });

      if (response.status !== 204) throw new HttpError(response.status);
    },
    onSuccess: () => {
      void qc.invalidateQueries({ queryKey: keys.objects() });
      void qc.invalidateQueries({ queryKey: keys.search() });
    },
    meta: { successMessage: "toast.deleted", suppressErrorToast: true },
  });
}

export function useSetVisibility() {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: async ({ id, visibility }: { id: string; visibility: Visibility }) => {
      const { response } = await api.POST("/api/admin/objects/{id}/visibility", {
        params: { path: { id } },
        body: { visibility },
      });

      if (response.status !== 204) throw new VisibilityError(response.status);
    },
    onSuccess: (_result, { id }) => {
      void qc.invalidateQueries({ queryKey: keys.object(id) });
      void qc.invalidateQueries({ queryKey: keys.objects() });
      void qc.invalidateQueries({ queryKey: keys.search() });
    },
    meta: { successMessage: "toast.published", suppressErrorToast: true },
  });
}
