import apiFetch from "../utils/api";

/**
 * Thin adapter that converts apiFetch responses into the shape TanStack Query expects.
 * - Checks response.ok and result.success
 * - Throws on errors so TanStack Query can handle retry/error states
 * - Returns result.data directly (unwrapped from the API envelope)
 */
export async function jsonQuery<T>(
  url: string,
  options?: RequestInit,
): Promise<T> {
  const response = await apiFetch(url, options);

  if (response.status === 401) {
    throw new Error("Unauthorized");
  }

  let result: { success: boolean; data?: unknown; error?: string };
  try {
    result = (await response.json()) as typeof result;
  } catch {
    throw new Error("Invalid JSON response");
  }

  if (!response.ok || !result.success) {
    throw new Error(result.error || `Request failed (${response.status})`);
  }

  return result.data as T;
}

export interface PaginationMeta {
  total: number;
  page: number;
  per_page: number;
  total_pages: number;
}

export interface PaginatedResult<T> {
  data: T[];
  pagination: PaginationMeta;
}

export async function paginatedJsonQuery<T>(
  url: string,
  options?: RequestInit,
): Promise<PaginatedResult<T>> {
  const response = await apiFetch(url, options);

  if (response.status === 401) {
    throw new Error("Unauthorized");
  }

  let result: {
    success: boolean;
    data?: unknown;
    error?: string;
    pagination?: PaginationMeta;
  };
  try {
    result = (await response.json()) as typeof result;
  } catch {
    throw new Error("Invalid JSON response");
  }

  if (!response.ok || !result.success) {
    throw new Error(result.error || `Request failed (${response.status})`);
  }

  const items = Array.isArray(result.data)
    ? result.data
    : ((result.data as { items?: T[] })?.items ?? []);
  const pagination = result.pagination ??
    (result.data as { pagination?: PaginationMeta })?.pagination ?? {
      total: items.length,
      page: 1,
      per_page: items.length,
      total_pages: 1,
    };

  return { data: items as T[], pagination };
}
