/**
 * FAQ Controller
 * Handles HTTP requests for FAQs
 */

const faqService = require('../services/faq.service');
const { catchAsyncErrors } = require('../middlewares/catchAsyncErrors');
const { translateMessage } = require('../utils/translation.utils');

/**
 * GET /api/v1/faq
 * Get all FAQs (public)
 */
exports.getAllFAQs = catchAsyncErrors(async (req, res) => {
    const { category, search, tags, page, limit, sortBy } = req.query;

    const result = await faqService.getAllFAQs(
        { category, search, tags, isActive: true },
        { page, limit, sortBy }
    );

    res.status(200).json({
        success: true,
        message: 'FAQs retrieved successfully',
        data: result.faqs,
        pagination: result.pagination
    });
});

/**
 * GET /api/v1/faq/grouped
 * Get FAQs grouped by category
 */
exports.getFAQsByCategory = catchAsyncErrors(async (req, res) => {
    const grouped = await faqService.getFAQsByCategory();

    res.status(200).json({
        success: true,
        message: 'FAQs retrieved successfully',
        data: grouped
    });
});

/**
 * GET /api/v1/faq/categories
 * Get FAQ categories with counts
 */
exports.getFAQCategories = catchAsyncErrors(async (req, res) => {
    const categories = await faqService.getFAQCategories();

    res.status(200).json({
        success: true,
        message: 'Categories retrieved successfully',
        data: categories
    });
});

/**
 * GET /api/v1/faq/search
 * Search FAQs
 */
exports.searchFAQs = catchAsyncErrors(async (req, res) => {
    const { q, limit } = req.query;

    if (!q) {
        return res.status(400).json({
            success: false,
            message: 'Search query is required'
        });
    }

    const faqs = await faqService.searchFAQs(q, limit);

    res.status(200).json({
        success: true,
        message: 'Search results retrieved successfully',
        data: faqs
    });
});

/**
 * GET /api/v1/faq/:id
 * Get single FAQ
 */
exports.getFAQById = catchAsyncErrors(async (req, res) => {
    const faq = await faqService.getFAQById(req.params.id);

    res.status(200).json({
        success: true,
        message: 'FAQ retrieved successfully',
        data: faq
    });
});

/**
 * POST /api/v1/faq/:id/helpful
 * Mark FAQ as helpful/not helpful
 */
exports.markFAQHelpful = catchAsyncErrors(async (req, res) => {
    const { helpful } = req.body;

    if (helpful === undefined) {
        return res.status(400).json({
            success: false,
            message: 'helpful field is required (true/false)'
        });
    }

    const faq = await faqService.markFAQHelpful(req.params.id, helpful);

    res.status(200).json({
        success: true,
        message: 'Thank you for your feedback',
        data: faq
    });
});

// ==========================================
// ADMIN ROUTES
// ==========================================

/**
 * GET /api/v1/admin/faq
 * Get all FAQs (admin - includes inactive)
 */
exports.adminGetAllFAQs = catchAsyncErrors(async (req, res) => {
    const { category, search, isActive, page, limit, sortBy } = req.query;

    const result = await faqService.getAllFAQs(
        { category, search, isActive: isActive !== undefined ? isActive === 'true' : undefined },
        { page, limit, sortBy }
    );

    res.status(200).json({
        success: true,
        message: 'FAQs retrieved successfully',
        data: result.faqs,
        pagination: result.pagination
    });
});

/**
 * POST /api/v1/admin/faq
 * Create FAQ
 */
exports.createFAQ = catchAsyncErrors(async (req, res) => {
    const faq = await faqService.createFAQ(req.body, req.user._id);

    res.status(201).json({
        success: true,
        message: translateMessage('FAQ_CREATED', req.language) || 'FAQ created successfully',
        data: faq
    });
});

/**
 * PUT /api/v1/admin/faq/:id
 * Update FAQ
 */
exports.updateFAQ = catchAsyncErrors(async (req, res) => {
    const faq = await faqService.updateFAQ(req.params.id, req.body, req.user._id);

    res.status(200).json({
        success: true,
        message: translateMessage('FAQ_UPDATED', req.language) || 'FAQ updated successfully',
        data: faq
    });
});

/**
 * DELETE /api/v1/admin/faq/:id
 * Delete FAQ
 */
exports.deleteFAQ = catchAsyncErrors(async (req, res) => {
    await faqService.deleteFAQ(req.params.id);

    res.status(200).json({
        success: true,
        message: translateMessage('FAQ_DELETED', req.language) || 'FAQ deleted successfully'
    });
});

/**
 * PATCH /api/v1/admin/faq/reorder
 * Update FAQ order
 */
exports.updateFAQOrder = catchAsyncErrors(async (req, res) => {
    const { orderUpdates } = req.body;

    if (!Array.isArray(orderUpdates)) {
        return res.status(400).json({
            success: false,
            message: 'orderUpdates must be an array of { id, order }'
        });
    }

    await faqService.updateFAQOrder(orderUpdates);

    res.status(200).json({
        success: true,
        message: 'FAQ order updated successfully'
    });
});

module.exports = exports;
