const cloudinary = require('cloudinary').v2;
const SystemConfigService = require('../services/systemConfig.service');

require('dotenv').config();

/**
 * Lấy cloudinary instance đã được cấu hình từ DB
 * Fallback về process.env nếu DB chưa có
 */
async function getCloudinary() {
    const cfg = await SystemConfigService.getConfig();
    cloudinary.config({
        cloud_name: cfg.cloudinaryName || process.env.CLOUD_DINARY_NAME,
        api_key: cfg.cloudinaryKey || process.env.CLOUD_DINARY_KEY,
        api_secret: cfg.cloudinarySecret || process.env.CLOUD_DINARY_SECRET,
    });
    return cloudinary;
}

module.exports = { getCloudinary };
