// Test Cloudinary connection
import { v2 as cloudinary } from "cloudinary";
import "dotenv/config";

const cloudName = process.env.CLOUDINARY_CLOUD_NAME?.trim();
const apiKey = process.env.CLOUDINARY_API_KEY?.trim();
const apiSecret = process.env.CLOUDINARY_API_SECRET?.trim();

console.log("Testing Cloudinary Configuration...");
console.log("Cloud Name:", cloudName ? `${cloudName.substring(0, 5)}...` : "NOT SET");
console.log("API Key:", apiKey ? `${apiKey.substring(0, 5)}...` : "NOT SET");
console.log("API Secret:", apiSecret ? "SET" : "NOT SET");

if (!cloudName || !apiKey || !apiSecret) {
  console.error("\n❌ ERROR: Missing Cloudinary credentials in .env file");
  process.exit(1);
}

// Configure Cloudinary
cloudinary.config({
  cloud_name: cloudName,
  api_key: apiKey,
  api_secret: apiSecret,
});

// Test connection by trying to ping Cloudinary
cloudinary.api.ping((error, result) => {
  if (error) {
    console.error("\n❌ ERROR: Cloudinary authentication failed!");
    console.error("Error:", error.message);
    console.error("\nPlease verify:");
    console.error("1. Your CLOUDINARY_CLOUD_NAME is correct");
    console.error("2. Your CLOUDINARY_API_KEY is correct");
    console.error("3. Your CLOUDINARY_API_SECRET is correct");
    console.error("4. There are no extra spaces in your .env file");
    console.error("5. The credentials match your Cloudinary dashboard");
    process.exit(1);
  } else {
    console.log("\n✅ SUCCESS: Cloudinary is configured correctly!");
    console.log("Status:", result.status);
    process.exit(0);
  }
});

