import toast from "react-hot-toast"
import { RIGOBOT_HOST } from "./constants"
import axios, { AxiosError } from "axios"

type TInteractiveCreationInputs = {
  courseInfo: string
  prevInteractions: string
}

export const interactiveCreation = async (
  inputs: TInteractiveCreationInputs
): Promise<any | null> => {
  try {
    const response = await axios.post(
      `${RIGOBOT_HOST}/v1/prompting/public/completion/390/`,
      {
        inputs: inputs,
        include_purpose_objective: false,
        execute_async: false,
      },
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    )

    return response.data
  } catch (error: unknown) {
    const err = error as AxiosError

    if (err.response?.status === 403) {
      toast.error("You've reached the limit. Please log in to continue.")
    } else {
      toast.error("Something went wrong while generating the course.")
    }

    return null
  }
}

export const isHuman = async (token: string) => {
  try {
    const body = {
      access_token: token,
    }
    const response = await axios.post(
      `${RIGOBOT_HOST}/v1/auth/verify/humanity`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    )

    return response.status === 200
  } catch (error) {
    console.error(error)
    return false
  }
}
