// authConfig.js
const hostname = window.location.hostname;
const protocol = window.location.protocol;

// Détection environnements
const isProduction = hostname === "mygta.ensup-adm.net";

// ✅ EXPORT : API URL
export const API_BASE_URL = "/api";

// ✅ EXPORT : MSAL Config - OPTIMISÉ POUR MOBILE iOS
export const msalConfig = {
    auth: {
        clientId: "4bb4cc24-bac3-427c-b02c-5d14fc67b561",
        authority: "https://login.microsoftonline.com/9840a2a0-6ae1-4688-b03d-d2ec291be0f9",
        redirectUri: isProduction
            ? "https://mygta.ensup-adm.net"
            : `${protocol}//${hostname}`,
        navigateToLoginRequestUrl: false, // ✅ false pour éviter double redirection
        postLogoutRedirectUri: isProduction
            ? "https://mygta.ensup-adm.net"
            : `${protocol}//${hostname}`,
    },
    cache: {
        cacheLocation: "localStorage",
        storeAuthStateInCookie: true,
    },
    system: {
        allowRedirectInIframe: false,
        allowNativeBroker: false,
        loggerOptions: {
            logLevel: "Verbose",
            piiLoggingEnabled: false,
        },
        windowHashTimeout: 25000,
        iframeHashTimeout: 25000,
        loadFrameTimeout: 25000,
        tokenRenewalOffsetSeconds: 300,
        asyncPopups: false,
    }
};

// ✅ EXPORT : Permissions Graph
export const loginRequest = {
    scopes: [
        "User.Read",
        "User.Read.All",
        "Group.Read.All",
        "GroupMember.Read.All",
        "Mail.Send",
    ],
    prompt: "select_account",
    responseMode: "fragment",
};

// ✅ Log de configuration au démarrage
console.log("🔧 Config Auth:", {
    hostname,
    protocol,
    API_BASE_URL,
    redirectUri: msalConfig.auth.redirectUri
});