import fs from 'fs';
import path from 'path';

const ROOT_DIR = path.resolve(process.cwd());
const DEFAULT_UPLOAD_DIR = path.join(ROOT_DIR, 'uploads');

export const UPLOAD_DIR = process.env.UPLOAD_DIR && process.env.UPLOAD_DIR.trim() !== ''
  ? path.resolve(process.env.UPLOAD_DIR)
  : DEFAULT_UPLOAD_DIR;

export function ensureUploadDir(): void {
  if (!fs.existsSync(UPLOAD_DIR)) {
    fs.mkdirSync(UPLOAD_DIR, { recursive: true });
  }
}

export const storageConfig = {
  gcp: {
    projectId: process.env.GCP_PROJECT_ID || '',
    bucketName: process.env.GCP_BUCKET_NAME || '',
    keyFile: process.env.GCP_KEY_FILE || './config/gcp-key.json',
  },
  
  // File upload settings
  maxFileSize: parseInt(process.env.MAX_FILE_SIZE_MB || '10', 10) * 1024 * 1024, // Convert MB to bytes
  allowedMimeTypes: [
    'application/pdf',
    'application/msword',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'application/vnd.ms-excel',
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'application/vnd.ms-powerpoint',
    'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    'image/jpeg',
    'image/jpg',
    'image/png',
    'image/gif',
  ],
  
  // Storage paths
  paths: {
    documents: 'documents',
    attachments: 'attachments',
    temp: 'temp',
  },
};
