import { handleApiError } from ".";

export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginResponse {
  id: string;
  email: string;
  full_name: string;
  profile_picture: string;
  access: string;
  refresh: string;
}

export const loginApi = async (req: LoginRequest) => {
  const response = await fetch("/api/v1/login", {
    method: "POST",
    body: JSON.stringify({
      email: req.email,
      password: req.password,
    }),
    headers: {
      "Content-Type": "application/json",
    },
  });

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

  const data: LoginResponse = await response.json();

  return data;
};
