export interface GeminiConfig {
	geminiApiKey: string;
	geminiModel: string; // e.g. "gemini-2.5-flash"
	imageModel: string; // e.g. "gemini-3.1-flash-preview-image"
}

let cachedConfig: GeminiConfig | null = null;

export async function loadConfig(): Promise<GeminiConfig | null> {
	if (cachedConfig) return cachedConfig;
	try {
		const res = await fetch("/config.json");
		if (!res.ok) return null;
		const data = await res.json();
		if (!data.geminiApiKey || !data.geminiModel || !data.imageModel)
			return null;
		cachedConfig = data as GeminiConfig;
		return cachedConfig;
	} catch {
		return null;
	}
}

/** For Node.js usage in build script */
export function loadConfigFromObject(obj: GeminiConfig): void {
	cachedConfig = obj;
}

export function resetConfigCache(): void {
	cachedConfig = null;
}
