import { hasUncaughtExceptionCaptureCallback } from "process";
import { api } from "../utils/define";
import { Etudiant } from "../classes/etudiant";
import { Enseignant } from "../classes/enseignant";

export class LoginControlleur {
  public async login(email: string, password: string) {
    let resp = await api("/api/v3/teacher/login", {
      email: email,
      password: password,
    });
    if (resp.code < 400) {
      return new Enseignant(
        resp.body.user.id,
        resp.body.user.first_name,
        resp.body.user.last_name,
        resp.body.token
      );
    }

    resp = await api("/api/v3/student/login", {
      email: email,
      password: password,
    });
    if (resp.code < 400) {
      return new Etudiant(
        resp.body.user.id,
        resp.body.user.first_name,
        resp.body.user.last_name,
        resp.body.token
      );
    }

    let msg = "Not a teacher or a student";
    console.warn(msg);
    throw { code: resp.code, msg: msg };
  }

  public async getUtilisateur(token: string) {
    let resp = await api("/api/v3/teacher/fromtoken", { token: token });
    if (resp.code < 400) {
      return new Enseignant(
        resp.body.user.id,
        resp.body.user.first_name,
        resp.body.user.last_name,
        token
      );
    }

    resp = await api("/api/v3/student/fromtoken", { token: token });
    if (resp.code < 400) {
      return new Etudiant(
        resp.body.user.id,
        resp.body.user.first_name,
        resp.body.user.last_name,
        token
      );
    }

    let msg = "Il n'y a pas d'étudiant ou d'enseignant associer au information";
    console.warn(msg);
    return false;
    throw {
      code: resp.code,
      isAnonyme: true,
      msg: "Il n'y a pas d'étudiant ou d'enseignant associer au information",
    };
  }
}
