/**
 * In-memory feature flag registry for product backends.
 *
 * Products call createFlagRegistry() with their default flags,
 * then use isFeatureEnabled/getAllFlags/setFlag as needed.
 */

export interface FlagRegistry {
  isFeatureEnabled(flag: string, userId?: string): boolean;
  getAllFlags(): Record<string, boolean>;
  setFlag(flag: string, value: boolean): void;
}

export interface FlagRegistryOptions {
  /** Default flag values. */
  defaults: Record<string, boolean>;
  /** Master switch — when false, flags are still resolved from defaults but
   *  the registry won't attempt remote/dynamic flag resolution (future use). */
  enabled?: boolean;
}

export function createFlagRegistry(opts: FlagRegistryOptions): FlagRegistry {
  const flags: Map<string, boolean> = new Map(Object.entries(opts.defaults));

  return {
    isFeatureEnabled(flag: string, _userId?: string): boolean {
      return flags.get(flag) ?? false;
    },

    getAllFlags(): Record<string, boolean> {
      return Object.fromEntries(flags);
    },

    setFlag(flag: string, value: boolean): void {
      flags.set(flag, value);
    },
  };
}
