/* graph.jsx — údaje o prihlásenom používateľovi z Microsoft Graph (len /me, scope User.Read).
   Vystavuje window.HUB_GRAPH = { getProfil(), getFotka() }.
*/
(function () {
  "use strict";

  const GRAPH = "https://graph.microsoft.com/v1.0";

  async function graphFetch(path) {
    const token = await window.HUB_AUTH.getToken();
    return fetch(GRAPH + path, { headers: { Authorization: "Bearer " + token } });
  }

  async function getProfil() {
    const res = await graphFetch("/me?$select=displayName,givenName,mail,userPrincipalName,department,jobTitle");
    if (!res.ok) throw new Error("Graph /me vrátil " + res.status);
    return res.json();
  }

  // Fotka je voliteľná — veľa účtov ju nemá (Graph vráti 404). Vtedy sa ukážu iniciály.
  // Vracia object URL, ktorý volajúci uvoľní cez URL.revokeObjectURL.
  async function getFotka() {
    try {
      const res = await graphFetch("/me/photos/96x96/$value");
      if (!res.ok) return null;
      return URL.createObjectURL(await res.blob());
    } catch (_) {
      return null;
    }
  }

  window.HUB_GRAPH = { getProfil, getFotka };
})();
