import type { UserProfile } from "@/types";
import { axios, handleApiError } from "..";

export interface FetchUsersResponse {
  items: UserProfile[];
  count: number;
}

export const adminGetUsersApi = async (): Promise<FetchUsersResponse> => {
  const response = await axios.get<FetchUsersResponse>("/api/v1/admin/users");

  if (response.status !== 200 && response.status !== 201)
    throw await handleApiError(response);

  return response.data;
};

export type FetchUserResponse = UserProfile;

export const adminGetUserApi = async (
  id: string,
): Promise<FetchUserResponse> => {
  const response = await axios.get<FetchUserResponse>(
    `/api/v1/admin/users/${id}`,
  );

  if (response.status !== 200 && response.status !== 201)
    throw await handleApiError(response);

  return response.data;
};

export interface CreateUserRequest {
  email: string;
  full_name: string;
  password: string;
  is_admin: boolean;
}

export interface CreateUserResponse {
  id: string;
}

export const postUser = async (
  req: CreateUserRequest,
): Promise<CreateUserResponse> => {
  const response = await axios.post<CreateUserResponse>(
    "/api/v1/admin/users",
    req,
  );

  if (response.status !== 200 && response.status !== 201)
    throw await handleApiError(response);

  return response.data;
};
