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 GetStudent(numAcount: number) {
  const cookieStore = cookies();
  const token = (await cookieStore).get("token")?.value;

  if (!token) redirect("/");

  try {
    const response = await axios.get(
      `${envConfig.apiUrl}/student/${numAcount}`,
      {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      }
    );

    return response.data;

  } catch (error: any) {
    return {
      error:
        error.response?.data?.message ||
        "Error al obtener estudiante",
      status: error.response?.status,
    };
  }
}
//IO
