import { fetchWithAuth } from "./client";

export const adminApi = {
  getStats: async (token: string) =>
    fetchWithAuth("/api/v1/admin/stats", {}, token),
  getUsers: async (
    token: string,
    params?: { skip?: number; limit?: number; search?: string }
  ) => {
    const query = params
      ? "?" +
        new URLSearchParams(
          Object.entries(params).filter(
            ([, v]) => v !== undefined
          ) as [string, string][]
        ).toString()
      : "";
    return fetchWithAuth(`/api/v1/admin/users${query}`, {}, token);
  },
  getUserDetail: async (token: string, userId: string) =>
    fetchWithAuth(`/api/v1/admin/users/${userId}`, {}, token),
  toggleUserActive: async (token: string, userId: string) =>
    fetchWithAuth(
      `/api/v1/admin/users/${userId}/toggle-active`,
      { method: "POST" },
      token
    ),
  updateUserPlan: async (token: string, userId: string, plan: string) =>
    fetchWithAuth(
      `/api/v1/admin/users/${userId}/update-plan`,
      {
        method: "PUT",
        body: JSON.stringify({ plan }),
      },
      token
    ),
};
