const userService = require("../service/user.service")
const validator = require("validator")

module.exports = {
    login,
    register,
    getUser,
    changePassword,
    changeAddress,
    changeName,
    reportUser
}
function login(req, res){
    let userInfo = req.body
    if(userInfo.email && userInfo.password){
        if(validator.isEmail(userInfo.email)){
            userService.login(userInfo.email, userInfo.password).then(user => {
                req.session.user = user
                // res.cookie("userName", user.userName, { maxAge: 36000000 }) // 10 hours
                // res.cookie("userID", user.userID, { maxAge: 36000000 })
                    res.json({userName: user.userName, userID: user.userID})
                }
            ).catch(err =>{
                res.status(400).json({message: "Username or password is incorrect"})

            })
        }
        else{
            res.status(400).json({message: "invalid email address"})
        }
    }
    else{
        res.status(400).json({message: "email address and password can't be empty"})
    }

}

function register(req, res){
    console.log(req.body)
    let userInfo = req.body
    if(userInfo.email && userInfo.password && userInfo.name){
        if(!validator.isEmail(userInfo.email)){
            res.status(400).json({message: "invalid email address"})
            return
        }
        if(userInfo.password.length < 7){
            res.status(400).json({message: "password is too short"})
            return
        }
        if(userInfo.password.length > 16){
            res.status(400).json({message: "password is too long"})
            return
        }
        if(!/[a-zA-Z]/.test(userInfo.password)){
            res.status(400).json({message: "password must have at least one letter"})
            return
        }
        if(!/[0-9]/.test(userInfo.password)){
            res.status(400).json({message: "password must have at least one number"})
            return
        }
        userService.addUser(userInfo.email, userInfo.password, userInfo.name)
            .then(()=> res.json({}))
            .catch(err => res.status(400).json({message: err}))
    }
    else{
        res.status(400).json({message: "email address, username, and password can't be empty"})
    }
}

async function getUser(req, res){
    if(!req.params.param){
        res.status(400).json({message: "user not exist"})
        return
    }
    if(isNaN(req.params.param)){
        res.status(400).json({message: "user not exist"})
        return
    }
    let user = await userService.getUserByID(req.params.param)
    if(!user){
        res.status(400).json({message: "user not exist"})
    }
    res.json(user)
}

async function changePassword(req, res){
    if(!req.body.password || req.body.newPassword || req.body.id){
        res.status(400).json({message: "email address and password can't be empty"})
        return
    }
    let newP = req.body.newPassword
    let oldP = req.body.password
    let id = req.body.id

    if(newP.length < 7){
        res.status(400).json({message: "password is too short"})
        return
    }
    if(newP.length > 16){
        res.status(400).json({message: "password is too long"})
        return
    }
    if(!/[a-zA-Z]/.test(newP)){
        res.status(400).json({message: "password must have at least one letter"})
        return
    }
    if(!/[0-9]/.test(newP)){
        res.status(400).json({message: "password must have at least one number"})
        return
    }
    userService.changePassword(newP, oldP, id).then(()=>res.json({}))
        .catch(err=>{
            res.status(400).json({message: err})
        })
}

async function changeAddress(req, res){
    let address = req.body.address
    let password = req.body.password
    let id = req.body.id
    if(!address){
        address = ""
    }
    if(!password || !id){
        res.status(400).json({message: "access denied"})
    }
    userService.changeAddress(address, password, id).then(()=>res.json({}))
        .catch(err=>{
            res.status(400).json({message: err})
        })
}

async function changeName(req, res){
    let name = req.body.name
    let password = req.body.password
    let id = req.body.id
    if(!name){
        res.status(400).json({message: "name can't be empty"})
    }
    if(!password || !id){
        res.status(400).json({message: "access denied"})
    }
    userService.changeName(name, password, id).then(()=>res.json({}))
        .catch(err=>{
            res.status(400).json({message: err})
        })
}

async function reportUser(req, res) {
    try {
        // Assuming the passed parameter is called 'id'
        const reportedUserId = req.params.id;
        const result = await userService.reportUser(reportedUserId);
        res.status(200).json({ message: 'User reported successfully', result });
    } catch (error) {
        res.status(500).json({ message: error.message });
    }
}

