const express = require("express");
const jwt = require("jsonwebtoken");
const multer = require("multer");

require("dotenv").config();

const userModel = require("../models/userModel");
const adminModel = require("../models/adminModel");

const verifyUser = async ( req, res, next) => {
    const bearerHeader = req.headers['authorization'];
    if( typeof bearerHeader !== "undefined"){
        const bearer = bearerHeader.split(" ")[1];
        req.token = bearer;
        jwt.verify(bearer, process.env.SECRET_KEY, async (error, authData)=> {
            if(error){
                console.log("Invalid Token :", error);
                return res.status(403).json({
                    msg: "Invalid Token !"
                })
            } else {
                let id = authData.id;
                let userData = await userModel.findById(id).select("-password");
                req.user = userData;
                next();
            }
        }) 
    } else {
        return res.status(403).json({
            msg: "Token Not Found!"
        })
    }
};

const verifyAdmin = async ( req, res, next) => {
    const bearerHeader = req.headers['authorization'];
    if( typeof bearerHeader !== "undefined"){
        const bearer = bearerHeader.split(" ")[1];
        req.token = bearer;
        jwt.verify(bearer, process.env.SECRET_KEY, async (error, authData)=> {
            if(error){
                console.log("Invalid Token :", error);
                return res.status(403).json({
                    msg: "Invalid Token !"
                })
            } else {
                let id = authData.id;
                let userData = await adminModel.findById(id).select("-password");
                req.user = userData;
                next();
            }
        }) 
    } else {
        return res.status(403).json({
            msg: "Token Not Found!"
        })
    }
};

const adminStorage = multer.diskStorage({
    destination: ( req, file, cb ) => {
        cb(null, "public/admin");
    },
    filename: ( req, file, cb ) => {
        cb(null, Date.now() + "-" + file.originalname);
    }
});

const uploadAdmin = multer({ storage: adminStorage });

const userStorage = multer.diskStorage({
    destination: ( req, file, cb ) => {
        cb(null, "public/user");
    },
    filename: ( req, file, cb ) => {
        cb(null, Date.now() + "-" + file.originalname );
    }
});

const uploadUser = multer({ storage: userStorage });

const shopStorage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb( null, "public/shop");
    },
    filename: (req, file, cb) => {
        cb(null, Date.now() + "-" + file.originalname);
    }
});

const uploadShop = multer({ storage: shopStorage });

const productStorage = multer.diskStorage({
    destination: ( req, file, cb ) => {
        cb(null, "public/product");
    },
    filename: ( req, file, cb ) => {
        cb(null, Date.now() + "-" + file.originalname)
    }
});

const uploadProduct = multer({ storage: productStorage });

module.exports = {
    verifyUser,
    verifyAdmin,
    uploadAdmin,
    uploadUser,
    uploadShop,
    uploadProduct
};