const cloudinary = require('cloudinary').v2;
const fs = require('fs');
const path = require('path');

cloudinary.config({
  cloud_name: 'du7xohbct',
  api_key: '525922573613433',
  api_secret: 'cJ0NDcaoQhSTAxBMv6jNMFupt3k'
});

const uploadImages = async () => {
  const foldersToUpload = ['kitehotel', 'kitesurf'];
  const results = {};

  for (const folderName of foldersToUpload) {
    const publicDir = path.join(__dirname, '..', 'public', folderName);
    
    if (!fs.existsSync(publicDir)) {
      console.log(`Directory does not exist: ${publicDir}`);
      continue;
    }

    const files = fs.readdirSync(publicDir);
    const images = files.filter(file => file.endsWith('.jpg') || file.endsWith('.jpeg') || file.endsWith('.png') || file.endsWith('.webp'));
    
    for (const file of images) {
      console.log(`Uploading ${folderName}/${file}...`);
      try {
        const filePath = path.join(publicDir, file);
        const result = await cloudinary.uploader.upload(filePath, {
          folder: `moygrup/${folderName}`,
          use_filename: true,
          unique_filename: false
        });
        console.log(`Uploaded ${file} -> ${result.secure_url}`);
        results[`${folderName}/${file}`] = result.secure_url;
      } catch (error) {
        console.error(`Error uploading ${folderName}/${file}:`, error);
      }
    }
  }
  
  console.log('\n--- Upload Results ---');
  console.log(JSON.stringify(results, null, 2));
};

uploadImages();
