/**
 * Hand-written wrappers for endpoints not yet picked up by orval regen.
 * Run `npm run api:gen` after running the backend to migrate to the generated client.
 */
import { privateMutator } from "../privateClient";
import type { Post } from "../generated/private/models/post";
import type { Message } from "../generated/private/models/message";

export interface CursorPaginated<T> {
  next: string | null;
  previous: string | null;
  results: T[];
}

export type FeedStrategy = "recent";

export interface FeedParams {
  cursor?: string | null;
  feed_strategy?: FeedStrategy;
  page_size?: number;
}

export const apiSocialPostsFeed = (
  params?: FeedParams,
  signal?: AbortSignal,
) =>
  privateMutator<CursorPaginated<Post>>({
    url: `/api/social/posts/feed/`,
    method: "GET",
    params,
    signal,
  });

export const feedQueryKey = (params?: Omit<FeedParams, "cursor">) =>
  ["social", "posts", "feed", params ?? {}] as const;

export interface RepliesParams {
  reply_to: number;
  cursor?: string | null;
  page_size?: number;
}

export const apiSocialPostReplies = (
  params: RepliesParams,
  signal?: AbortSignal,
) =>
  privateMutator<CursorPaginated<Post>>({
    url: `/api/social/posts/`,
    method: "GET",
    params,
    signal,
  });

export const repliesQueryKey = (postId: number) =>
  ["social", "posts", "replies", postId] as const;

export interface MessagesParams {
  chat: number;
  cursor?: string | null;
  page_size?: number;
}

export const apiSocialMessagesCursor = (
  params: MessagesParams,
  signal?: AbortSignal,
) =>
  privateMutator<CursorPaginated<Message>>({
    url: `/api/social/messages/`,
    method: "GET",
    params,
    signal,
  });

export const messagesQueryKey = (chatId: number) =>
  ["social", "messages", chatId] as const;
