/**
 * Storage Configuration for LocalGreenChain
 * Agent 3: File Upload & Storage System
 *
 * Supports multiple storage providers: AWS S3, Cloudflare R2, MinIO, or local filesystem
 */

import { StorageConfig, StorageProvider } from './types';

function getStorageProvider(): StorageProvider {
  const provider = process.env.STORAGE_PROVIDER as StorageProvider;
  if (provider && ['s3', 'r2', 'minio', 'local'].includes(provider)) {
    return provider;
  }
  return 'local'; // Default to local storage for development
}

export function getStorageConfig(): StorageConfig {
  const provider = getStorageProvider();

  const baseConfig: StorageConfig = {
    provider,
    bucket: process.env.STORAGE_BUCKET || 'localgreenchain',
    region: process.env.STORAGE_REGION || 'us-east-1',
    accessKeyId: process.env.STORAGE_ACCESS_KEY_ID,
    secretAccessKey: process.env.STORAGE_SECRET_ACCESS_KEY,
    publicUrl: process.env.STORAGE_PUBLIC_URL,
  };

  switch (provider) {
    case 's3':
      return {
        ...baseConfig,
        endpoint: process.env.AWS_S3_ENDPOINT,
      };

    case 'r2':
      return {
        ...baseConfig,
        endpoint: process.env.CLOUDFLARE_R2_ENDPOINT ||
          `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
        region: 'auto',
      };

    case 'minio':
      return {
        ...baseConfig,
        endpoint: process.env.MINIO_ENDPOINT || 'http://localhost:9000',
        region: 'us-east-1',
      };

    case 'local':
    default:
      return {
        ...baseConfig,
        localPath: process.env.LOCAL_STORAGE_PATH || './uploads',
        publicUrl: process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3001',
      };
  }
}

export const storageConfig = getStorageConfig();

/**
 * Environment variable template for storage configuration:
 *
 * # Storage Provider (s3, r2, minio, local)
 * STORAGE_PROVIDER=local
 * STORAGE_BUCKET=localgreenchain
 * STORAGE_REGION=us-east-1
 * STORAGE_ACCESS_KEY_ID=your-access-key
 * STORAGE_SECRET_ACCESS_KEY=your-secret-key
 * STORAGE_PUBLIC_URL=https://cdn.yourdomain.com
 *
 * # For AWS S3
 * AWS_S3_ENDPOINT=https://s3.amazonaws.com
 *
 * # For Cloudflare R2
 * CLOUDFLARE_ACCOUNT_ID=your-account-id
 * CLOUDFLARE_R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
 *
 * # For MinIO
 * MINIO_ENDPOINT=http://localhost:9000
 *
 * # For Local Storage
 * LOCAL_STORAGE_PATH=./uploads
 */
