import { useConfig } from "@/features/providers/config";

export enum FEATURE_KEYS {
    DRIVE = 'drive',
    AI_SUMMARY = 'ai_summary',
    AI_AUTOLABELS = 'ai_autolabels',
    MAILBOX_ADMIN_CHANNELS = 'mailbox_admin_channels',
    MAILDOMAIN_CREATE = 'maildomain_create',
    MAILDOMAIN_MANAGE_ACCESSES = 'maildomain_manage_accesses',
    MAILDOMAIN_MANAGE_TOTP = 'maildomain_manage_totp',
    THREAD_SPLIT = 'thread_split',
    MAILBOX_STATS = 'mailbox_stats',
}

/**
 * A hook to check if a feature is enabled.
 *
 * Several features like ai features or interoperability can be
 * enabled/disabled according to the config. This utility hook
 * to know the state of a feature with ease.
 */
export const useFeatureFlag = (featureKey: FEATURE_KEYS) => {
    const config = useConfig();

    switch (featureKey) {
        case FEATURE_KEYS.DRIVE:
            return Boolean(config.DRIVE.sdk_url);
        case FEATURE_KEYS.AI_SUMMARY:
            return config.AI_ENABLED === true && config.FEATURE_AI_SUMMARY === true;
        case FEATURE_KEYS.AI_AUTOLABELS:
            return config.AI_ENABLED === true && config.FEATURE_AI_AUTOLABELS === true;
        case FEATURE_KEYS.MAILBOX_ADMIN_CHANNELS:
            return Array.isArray(config.FEATURE_MAILBOX_ADMIN_CHANNELS) && config.FEATURE_MAILBOX_ADMIN_CHANNELS.length > 0;
        case FEATURE_KEYS.MAILDOMAIN_CREATE:
            return config.FEATURE_MAILDOMAIN_CREATE === true;
        case FEATURE_KEYS.MAILDOMAIN_MANAGE_ACCESSES:
            return config.FEATURE_MAILDOMAIN_MANAGE_ACCESSES === true;
        case FEATURE_KEYS.MAILDOMAIN_MANAGE_TOTP:
            return config.FEATURE_MAILDOMAIN_MANAGE_TOTP === true;
        case FEATURE_KEYS.THREAD_SPLIT:
            return config.FEATURE_THREAD_SPLIT === true;
        case FEATURE_KEYS.MAILBOX_STATS:
            return config.FEATURE_MAILBOX_STATS === true;
        default:
            throw new Error(`Unknown feature key: ${featureKey}`);
    }
}
