import type { StorageProvider } from '../types';
import { createLocalStorageProvider } from './local-provider';
import { createS3StorageProvider } from './s3-provider';

let cachedProvider: StorageProvider | null = null;

export function resolveStorageProvider(): StorageProvider {
  if (cachedProvider) {
    return cachedProvider;
  }

  const provider = process.env.STORAGE_PROVIDER?.trim() || 'local';

  switch (provider) {
    case 'local':
      cachedProvider = createLocalStorageProvider();
      return cachedProvider;
    case 's3':
      cachedProvider = createS3StorageProvider();
      return cachedProvider;
    default:
      throw new Error(`Unsupported storage provider: ${provider}`);
  }
}
