import axios from "axios";
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 axios.post("/api/v1/auth/login", {
    email: req.email,
    password: req.password,
  });

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

  const data: LoginResponse = response.data;

  return data;
};
