const discussionFormFunction = require("../functions/discussionForm");

const createDiscussionForm = async (req, res) => {
    try {
        const form = await discussionFormFunction.createDiscussionForm(req);
        return res.status(200).json({
            success: true,
            msg: "Disccussion Form Created!",
            data: form
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })  
    }
};

const getDiscussionFormById = async (req, res) => {
    try {
        const form = await discussionFormFunction.getDiscussionFormById(req);
        if(!form){
            return res.status(200).json({
                success: false,
                msg: "Discussion Form Not Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Discussion Form Details By Id!",
                data: form
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })  
    }    
};

const getAllDiscussionForms = async (req, res) => {
    try {
        const forms = await discussionFormFunction.getAllDiscussionForms(req);
        if(forms.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Discussion Forms Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Discussion Forms",
                data: forms
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })  
    }    
};

const updateDiscussionForm = async (req, res) => {
    try {
        const form = await discussionFormFunction.updateDiscussionForm(req);
        if(!form){
            return res.status(200).json({
                success: false,
                msg: "No Discussion Form Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Discussion Form Updated!",
                data: form
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })  
    }    
};

const deleteDiscussionForm = async (req, res) => {
    try {
        const form = await discussionFormFunction.deleteDiscussionForm(req);
        if(!form){
            return res.status(200).json({
                success: false,
                msg: "No Discussion Form Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Discussion Form Deleted!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })  
    }    
};

module.exports = {
    createDiscussionForm,
    getDiscussionFormById,
    getAllDiscussionForms,
    updateDiscussionForm,
    deleteDiscussionForm
};