/**
 * Factory — create an EmbeddingProvider from a persisted EmbeddingConfig.
 */

import type { EmbeddingProvider } from './provider.js';
import { NoopEmbeddingProvider } from './provider.js';
import { OpenAIEmbeddingProvider } from './openai.provider.js';
import { LocalEmbeddingProvider } from './local.provider.js';

export interface EmbeddingConfig {
	provider: 'openai' | 'local' | 'none';
	openai?: {
		baseUrl: string;
		apiKey: string;
		model: string;
		dimensions?: number;
		maxBatchSize?: number;
	};
}

/** The settings table key used to persist the embedding configuration. */
export const EMBEDDING_CONFIG_KEY = 'embedding_config';

/**
 * Construct the appropriate EmbeddingProvider for the given config.
 * Falls back to NoopEmbeddingProvider for unknown or missing providers.
 */
export function createProviderFromConfig(config: EmbeddingConfig): EmbeddingProvider {
	switch (config.provider) {
		case 'openai': {
			if (!config.openai) {
				throw new Error('OpenAI provider selected but no openai config provided.');
			}
			return new OpenAIEmbeddingProvider(config.openai);
		}
		case 'local': {
			return new LocalEmbeddingProvider();
		}
		case 'none':
		default:
			return new NoopEmbeddingProvider();
	}
}

/**
 * Return a default (noop) config when nothing has been stored yet.
 */
export function defaultEmbeddingConfig(): EmbeddingConfig {
	return { provider: 'none' };
}

export type { EmbeddingProvider };
