/**
 * Google Cloud Storage Image Uploader (Simplified)
 * 
 * This script uploads all images from a local folder to a Google Cloud Storage bucket
 * with a common prefix to simulate a folder structure.
 * 
 * Prerequisites:
 * - Node.js installed
 * - Google Cloud SDK configured or service account key available
 * - @google-cloud/storage package installed (npm install @google-cloud/storage)
 */

const { Storage } = require('@google-cloud/storage');
const fs = require('fs');
const path = require('path');

// Configuration - replace these values with your own
const BUCKET_NAME = 'dev-banking-jago-partner';
const PREFIX = 'temp-stockbit-ktp-images/'; // This simulates a folder in GCS (e.g., 'images/')
const LOCAL_IMAGES_FOLDER = './stockbit-ktp'; // Path to your local images folder
const IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.bmp', '.tiff'];

// Initialize Google Cloud Storage
const storage = new Storage();
const bucket = storage.bucket(BUCKET_NAME);

/**
 * Checks if a file is an image based on its extension
 * @param {string} filePath - Path to the file
 * @returns {boolean} - True if the file is an image
 */
function isImage(filePath) {
  const ext = path.extname(filePath).toLowerCase();
  return IMAGE_EXTENSIONS.includes(ext);
}

/**
 * Uploads a file to Google Cloud Storage
 * @param {string} localFilePath - Path to the local file
 * @param {string} destinationFileName - Name to give the file in GCS
 */
async function uploadFile(localFilePath, destinationFileName) {
  try {
    await bucket.upload(localFilePath, {
      destination: destinationFileName,
      // Optional: set metadata
      metadata: {
        cacheControl: 'public, max-age=31536000',
      },
    });
    console.log(`Uploaded ${localFilePath} to ${destinationFileName}`);
    return true;
  } catch (error) {
    console.error(`Error uploading ${localFilePath}:`, error);
    return false;
  }
}

/**
 * Main function to upload all images
 */
async function uploadImagesToGcs() {
  console.log('Starting image upload process...');
  
  // Check if local folder exists
  if (!fs.existsSync(LOCAL_IMAGES_FOLDER)) {
    console.error(`Local folder ${LOCAL_IMAGES_FOLDER} does not exist!`);
    return;
  }
  
  // Read all files from local folder
  const files = fs.readdirSync(LOCAL_IMAGES_FOLDER);
  const imageFiles = files.filter(file => 
    isImage(file) && fs.statSync(path.join(LOCAL_IMAGES_FOLDER, file)).isFile()
  );
  
  console.log(`Found ${imageFiles.length} images to upload`);
  
  if (imageFiles.length === 0) {
    console.log('No images found to upload. Make sure your local folder contains image files.');
    return;
  }
  
  // Upload each image
  const uploadPromises = imageFiles.map(file => {
    const localFilePath = path.join(LOCAL_IMAGES_FOLDER, file);
    const destinationPath = `${PREFIX}${file}`;
    return uploadFile(localFilePath, destinationPath);
  });
  
  // Wait for all uploads to complete
  const results = await Promise.all(uploadPromises);
  
  // Count successes
  const successCount = results.filter(result => result).length;
  console.log(`Upload complete. Successfully uploaded ${successCount} of ${imageFiles.length} images.`);
}

// Execute the main function
uploadImagesToGcs().catch(err => {
  console.error('Unhandled error in upload process:', err);
});