const orderFunction = require("../function/order");
const notficationFunction = require("../function/notification");

const createOrder = async (req, res) => {
    try {
        const order = await orderFunction.createOrder(req);
        if(order){
            const products = order.product; 
            const updateProduct = await orderFunction.udpateProductInventory(products);

            const notificationData = {
            userId: order.userId,
            adminId: order.adminId,
            orderId: order._id,
            message:`Your Order Created! Status:${order.status}!`
            };

            const createNoti = await notficationFunction.createNotification(notificationData);
            console.log("Notification Created!", createNoti);
            return res.status(200).json({
                success: true,
                msg: "Order is Placed Waiting to be Accepted!",
                data: order
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Order is Not Placed due to some Reasons!",
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const getOrderById = async (req, res) => {
    try {
        const order = await orderFunction.getOrderById(req);
        return res.status(200).json({
            success: true,
            msg: "Order Details!",
            data: order
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const getAllordersByUserId = async (req, res) => {
    try {
        const orders = await orderFunction.getAllordersByUserId(req);
        if( orders.length === 0 ){
            return res.status(200).json({
                success: true,
                msg: "No Order Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Orders By UserId!",
                data: orders
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const getAllOrdersByAdminId = async (req, res) => {
    try {
        const orders = await orderFunction.getAllOrdersByAdminId(req);
        if( orders.length === 0){
            return res.status(200).json({
                success: true,
                msg: "No Orders Found!",
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Orders By AdminId!",
                data: orders
            })             
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const updateOrderStatus = async (req, res) => {
    try {
        const order = await orderFunction.updateOrderStatus(req);
         const notificationData = {
            userId: order.userId,
            adminId: order.adminId,
            orderId: order._id,
            message:`Your Order Status updated! Status: ${order.status}!`
            };
            const createNoti = await notficationFunction.createNotification(notificationData);
            console.log("Notification Created!", createNoti);
        return res.status(200).json({
            success: true,
            msg: "Order Status Is Updated!",
            data: order
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const deleteOrder = async (req, res) => {
    try {
        const order = await orderFunction.deleteOrder(req);
        return res.status(200).json({
            success: true,
            msg: "Order Is Deleted Successfully!"
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const getNotifications = async (req, res) => {
    try {
        const notification = await notficationFunction.getNotifications(req);
        if(notification.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Notification Found"
            })
        } else {
        return res.status(200).json({
            success: true,
            msg: "All Notifications!",
            data: notification
        })
        }
    } catch (error) {
        console.log("Having Error :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const updateOrder = async (req, res) => {
    try {
        const order = await orderFunction.updateOrder(req);
        if(!order){
            return res.status(200).json({
                success: false,
                msg: "Order not Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Order Details Updated!",
                data: order
            })
        }
    } catch (error) {
        console.log("Having Error :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }
}; 

const getAllOrders = async (req, res) => {
    try {
        const orders = await orderFunction.getAllOrders(req);
        if(orders.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Orders Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Orders!",
                data: orders
            })
        }
    } catch (error) {
        console.log("Having Error :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const StatisticsByVendor = async (req, res) => {
    try {
        const stats = await orderFunction.vendorStatistics(req);
        return res.status(200).json({
            success: true,
            msg: "All Statistics By Admin Id!",
            data: stats
        })
    } catch (error) {
                console.log("Having Error :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

module.exports = {
    createOrder,
    getOrderById,
    getAllordersByUserId,
    getAllOrdersByAdminId,
    updateOrderStatus,
    deleteOrder,
    getNotifications,
    updateOrder,
    getAllOrders,
    StatisticsByVendor
};