const cmsService = require('../services/cms.service');
// Translation utils removed

// ============================================================================
// Public Getters
// ============================================================================

/**
 * GET /api/v1/cms/banner
 * Get all active banners
 */
const getBanners = async (req, res) => {
    try {
        const banners = await cmsService.getBanners();

        // Handle null/undefined response
        if (!banners) {
            return res.status(200).json([]);
        }

        res.status(200).json(banners);
    } catch (error) {
        res.status(500).json({ message: 'Error fetching banners', error: error.message });
    }
};

/**
 * GET /api/v1/cms/footer
 * Get footer content
 */
const getFooter = async (req, res) => {
    try {
        const footer = await cmsService.getFooter();

        // Handle null/undefined response
        if (!footer) {
            return res.status(200).json([]);
        }

        res.status(200).json(footer);
    } catch (error) {
        res.status(500).json({ message: 'Error fetching footer content', error: error.message });
    }
};

// ==========================================
// ADMIN CONTROLLERS (No translation needed - return source data)
// ==========================================

// --- Banners ---
const createBanner = async (req, res) => {
    try {
        const banner = await cmsService.createBanner(req.body);
        res.status(201).json({ success: true, data: banner });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error creating banner', error: error.message });
    }
};

const updateBanner = async (req, res) => {
    try {
        const banner = await cmsService.updateBanner(req.params.id, req.body);
        if (!banner) return res.status(404).json({ success: false, message: 'Banner not found' });
        res.status(200).json({ success: true, data: banner });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error updating banner', error: error.message });
    }
};

const deleteBanner = async (req, res) => {
    try {
        const banner = await cmsService.deleteBanner(req.params.id);
        if (!banner) return res.status(404).json({ success: false, message: 'Banner not found' });
        // Translation deletion removed

        res.status(200).json({ success: true, message: 'Banner deleted successfully' });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error deleting banner', error: error.message });
    }
};

// ============================================================================
// NOTE: FAQ management has been moved to dedicated FAQ routes
// Use /api/v1/faq/* endpoints for advanced FAQ management
// See faq.controller.js and faq.routes.js
// ============================================================================

// --- Footer ---
const createFooterSection = async (req, res) => {
    try {
        const section = await cmsService.createFooterSection(req.body);
        res.status(201).json({ success: true, data: section });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error creating footer section', error: error.message });
    }
};

const updateFooterSection = async (req, res) => {
    try {
        const section = await cmsService.updateFooterSection(req.params.id, req.body);
        if (!section) return res.status(404).json({ success: false, message: 'Footer section not found' });
        res.status(200).json({ success: true, data: section });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error updating footer section', error: error.message });
    }
};

const deleteFooterSection = async (req, res) => {
    try {
        const section = await cmsService.deleteFooterSection(req.params.id);
        if (!section) return res.status(404).json({ success: false, message: 'Footer section not found' });
        // Translation deletion removed

        res.status(200).json({ success: true, message: 'Footer section deleted successfully' });
    } catch (error) {
        res.status(500).json({ success: false, message: 'Error deleting footer section', error: error.message });
    }
};

module.exports = {
    getBanners,
    getFooter,
    createBanner,
    updateBanner,
    deleteBanner,
    createFooterSection,
    updateFooterSection,
    deleteFooterSection
};
