import axios from "axios";
import type { GetGoogleAuthUrlResponse, LogoutResponse } from "../types/api/auth";
import type { ApiError } from "../types/api/error";
import { GetBackendUrl } from "./environment";

const BACKEND_URL = GetBackendUrl();


export async function GetGoogleAuthUrl(): Promise<string | ApiError> {
  const response = await axios.get<GetGoogleAuthUrlResponse>(`${BACKEND_URL}/v2/api/auth/login`);

  if (response.status !== 200) {
    const err: ApiError = {
      status: response.status,
      message: "[FAIL] Something went wrong."
    };
    return err;
  }

  return response.data.url;
}

export async function Logout(): Promise<void> {
  const response = await axios.get<LogoutResponse>(`${BACKEND_URL}/v2/api/auth/logout`);

  // This should never happen
  if (response.status !== 204)
    console.error("LOGOUT FAILED");
}
