import { threadsGraphGet } from "./client";
import type { ThreadsApiCredentials } from "./types";

export interface ThreadsOwnPost {
  id: string;
  text?: string;
  permalink?: string;
  timestamp?: string;
  media_type?: string;
}

interface ThreadsOwnPostsResponse {
  data?: ThreadsOwnPost[];
}

export async function getOwnPostsViaThreadsApi(
  credentials: ThreadsApiCredentials,
  limit = 25
): Promise<ThreadsOwnPost[]> {
  const json = await threadsGraphGet<ThreadsOwnPostsResponse>(
    `/${credentials.userId}/threads`,
    {
      access_token: credentials.accessToken,
      fields: "id,text,permalink,timestamp,media_type",
      limit: String(limit),
    }
  );

  return json.data ?? [];
}
