import { LocalStorageProvider } from './local.js'
import type { StorageProvider } from './provider.js'

export type { StorageProvider }

export function createStorageProvider(): StorageProvider {
  const provider = process.env.STORAGE_PROVIDER ?? 'local'

  if (provider === 'local') {
    const root = process.env.STORAGE_LOCAL_PATH ?? './data/files'
    const baseUrl = `http://localhost:${process.env.PORT ?? '8000'}`
    return new LocalStorageProvider(root, baseUrl)
  }

  if (provider === 's3') {
    // Lazy import to avoid requiring @aws-sdk when using local
    throw new Error(
      'S3 provider requires @aws-sdk/client-s3. Install it and update this factory.',
    )
  }

  throw new Error(`Unknown storage provider: ${provider}`)
}
