const GalleryImage = require('../../models/getGalleyImage');

const getGalleryImage = async (req, res) => {
    try {
        // Extract query parameters for filtering (if provided)
        const { gameName, childId, gameID} = req.query;

        // Build a filter object based on the query parameters
        const filter = {};

        if (gameName) {
            filter.gameName = gameName;
        }

        if (childId) {
            filter.childId = childId;
        }
        if (gameID) {
            filter.gameID = gameID;
        }

        // Fetch gallery images with filters and selected fields
        const galleryImages = await GalleryImage.find(filter).select('gameName childId gameID screenshotUrl');   // Select only these fields

        if (!galleryImages.length) {
            return res.status(404).json({ message: 'No images found' });
        }

        return res.status(200).json(galleryImages);  // Return the filtered gallery images with selected fields
    } catch (error) {
        console.error('Error fetching images:', error.message);
        return res.status(500).json({ error: 'Something went wrong' });
    }
};

module.exports = getGalleryImage;