import Mail from "../../models/mail.js";
import { GetClientApiAuthMiddleware } from "../middlewares/client.js";
import Stacker from "../middlewares/stacker.js";
import { Request, Response } from "express";
import Logging from "@hibas123/nodelogging";

export default Stacker(GetClientApiAuthMiddleware(), async (req: Request, res) => {
   const mode = req.query.mode;
   let mails = await Promise.all(
      req.user.mails.map((id) => Mail.findById(id))
   );

   let mail = mails.find((e) => e.primary) || mails[0];

   let base_response = {
      user_id: req.user.uid,
      id: req.user.uid,
      ID: req.user.uid,
      sub: req.user.uid,
      email: mail.mail,
      username: req.user.username,
      displayName: req.user.name,
      displayNameClaim: req.user.name,
   }

   if (mode == "nextcloud") {
      Logging.debug("Profile in Nextcloud mode");
      base_response["ocs"] = {
         data: {
            id: base_response.user_id,
            email: base_response.email,
            "display-name": base_response.displayName,
         }
      }
   }

   res.json(base_response);
})
