import axios from "axios";
import { envConfig } from "./config";
import { redirect } from "next/navigation";
import { cookies } from "next/headers";

if (!envConfig.apiUrl) {
  console.error("API URL is not defined in envConfig");
}

export async function GetRegisterStudent(numAcount: number) {
  const cookieStore = cookies();
  const token = (await cookieStore).get("token")?.value;

  if (!token) redirect("/");
  try {
    const response = await axios.get(
      `${envConfig.apiUrl}/alumno-inscrito/${numAcount}`,
      {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      }
    );

    return response.data;
  } catch (error: any) {
    const msg =
      error.response?.data?.message ||
      error.message ||
      "Error desconocido al obtener estudiante";
    return { error: msg };
  }
}
//IO
