const reviewFunction = require("../functions/review");
const userFunction = require("../functions/user");

const addReview = async (req, res) => {
    try {
        const review = await reviewFunction.createReview(req);
        if(review){
            const rating = await reviewFunction.avgTechnicianRating(req);
            const { technicianId } = req.body;
            const data = {
                technicianId,
                avgRating: rating.avgRating,
                totalReviews: rating.totalCount
            };
            const technician = await userFunction.updatedRatings(data);
            return res.status(200).json({
                success: true,
                msg: "Review Added Successfully!",
                data: review
            })
        } else {
            return res.status(200).json({
                success: false,
                msg: "Review Not Added!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })
    }
};

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

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

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

const deleteReview = async (req, res) => {
    try {
        const review = await reviewFunction.deleteReview(req);
        if(!review){
            return res.status(200).json({
                success: false,
                msg: "No Review Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Review Deleted Successfully!"
            })
        }                      
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })
    }    
};

module.exports = {
    addReview,
    getReviewById,
    getAllReviews,
    updateReview,
    deleteReview
};