/**
 * Host Detection Utility
 * 
 * Erkennt, ob App auf public oder internal Host läuft
 * Basiert auf window.location.hostname + env-config
 * 
 * @module Utils/hostDetection
 */

/**
 * Hole Host-Konfiguration und Feature-Flags
 * @returns {Object} Host-Config mit Feature-Flags
 */
export const getHostConfig = () => {
    const hostname = window.location.hostname;
    
    // Hole Hosts aus Runtime-Config (wird von env.sh beim Container-Start gesetzt)
    const publicHost = window._env_?.PUBLIC_HOST || 'deinprojekt.hobbyhimmel.de';
    const internalHost = window._env_?.INTERNAL_HOST || 'deinprojekt.lan.hobbyhimmel.de';

    // Bestimme Host-Typ
    const isPublic = hostname === publicHost;
    const isInternal = hostname === internalHost || hostname === 'localhost' || hostname === '127.0.0.1';

    // Feature Flags basierend auf Host
    return {
        hostname,
        publicHost,
        internalHost,
        isPublic,
        isInternal,
        
        // Feature Flags
        canAccessAdmin: isInternal,
        canAccessSlideshow: isInternal,
        canAccessGroups: isInternal,
        canAccessModeration: isInternal,
        canAccessReorder: isInternal,
        canAccessBatchUpload: isInternal,
        canAccessSocialMedia: isInternal,
        canAccessMigration: isInternal,
        
        // Immer erlaubt (public + internal)
        canUpload: true,
        canManageByUUID: true
    };
};

/**
 * Prüft, ob App auf public Host läuft
 * @returns {boolean} True wenn public Host
 */
export const isPublicHost = () => {
    return getHostConfig().isPublic;
};

/**
 * Prüft, ob App auf internal Host läuft
 * @returns {boolean} True wenn internal Host
 */
export const isInternalHost = () => {
    return getHostConfig().isInternal;
};

/**
 * Hole spezifisches Feature-Flag
 * @param {string} featureName - Name des Features (z.B. 'canAccessAdmin')
 * @returns {boolean} True wenn Feature erlaubt
 */
export const canAccessFeature = (featureName) => {
    const config = getHostConfig();
    return config[featureName] || false;
};

/**
 * Debug-Funktion: Logge Host-Config in Console
 * Nur in Development
 */
export const logHostConfig = () => {
    if (process.env.NODE_ENV === 'development') {
        const config = getHostConfig();
        console.log('🔍 Host Configuration:', {
            hostname: config.hostname,
            isPublic: config.isPublic,
            isInternal: config.isInternal,
            features: {
                admin: config.canAccessAdmin,
                slideshow: config.canAccessSlideshow,
                groups: config.canAccessGroups,
                moderation: config.canAccessModeration
            }
        });
    }
};
