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

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

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

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

const updateRequestForm = async (req, res) => {
    try {
        const form = await requestFormFunction.updateFormById(req);
        if(!form){
            return res.status(200).json({
                success: false,
                msg: "No Request Form Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Request 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 deleteRequestForm = async (req, res) => {
    try {
        const form = await requestFormFunction.deleteForm(req);
        if(!form){
            return res.status(200).json({
                success: false,
                msg: "No Request Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Request 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 = {
    createRequestForm,
    getRequestFormById,
    getAllRequestForms,
    updateRequestForm,
    deleteRequestForm
};