const SQL = require("../_helper/database")
const bcrypt = require("bcryptjs")

module.exports= {
    login,
    addUser,
    getUserByID,
    getUserByEmail,
    changeAddress,
    changePassword,
    changeName,
    reportUser
}

// TODO: 创一个 session with 用户名和 history
async function login(email, password){
    let user = await getUserByEmail(email)
    if(!user || !bcrypt.compareSync(password, user.password)){
        throw "email or password in correct"
    }
    if (user.status === 'reported') {
        throw new Error('This account has been reported and cannot log in at this time.');
    }
    return {userID: user.id, userEmail: email, passwordHash: user.password, userName: user.name}
}

async function sendConformationEmail(email){

}

async function addUser(email, password, name){
    let user = await getUserByEmail(email)
    if(user){
        throw `email address ${email} is already registered`
    }

    let db = await SQL.getConnection()
    let isVT = false
    let reported = false
    if(email.endsWith("@vt.edu")){
        isVT = true
    }
    let hash = bcrypt.hashSync(password, 10)
    await db.execute(`
        INSERT INTO users (name, email, password, vt_member, reported) VALUES
        ('${name}', '${email}', '${hash}', ${isVT}, ${reported})
    `)
}

async function getUserByEmail(email){
    let db = await SQL.getConnection()
    let command = `SELECT * FROM users WHERE email = "${email}"`
    let res = await db.query(command)

    if(res[0].length === 0){
        return null
    }
    return res[0][0]
}

async function getUserByID(id, password=false){
    let db = await SQL.getConnection()
    let res = null
    if(password){
        res = await db.query(`SELECT * FROM users WHERE id = ${id}`)
    }
    else{
        res = await db.query(`SELECT name, email, vt_member, address, phone FROM users WHERE id = ${id}`)
    }

    if(res[0].length === 0){
        return null
    }
    return res[0][0]
}

async function changeName(name, password, userID){
    let user = await getUserByID(userID)
    if (!user){
        throw "user not exist"
    }
    if(password !== user.password){
        throw "access denied"
    }
    let db = await SQL.getConnection()
    await db.query(`UPDATE users SET name=${name} WHERE id = ${userID}`)
}

async function changePassword(newPassword, oldPassword, userID){
    let user = await getUserByID(userID)
    if (!user){
        throw "user not exist"
    }
    if(oldPassword !== user.password){
        throw "access denied"
    }
    let db = await SQL.getConnection()
    await db.query(`UPDATE users SET password=${newPassword} WHERE id = ${userID}`)
}

async function changeAddress(address, password, userID){
    let user = await getUserByID(userID)
    if (!user){
        throw "user not exist"
    }
    if(password !== user.password){
        throw "access denied"
    }
    let db = await SQL.getConnection()
    await db.query(`UPDATE users SET address=${address} WHERE id = ${userID}`)
}

async function reportUser(reportedUserId) {
    // Update the reported user's status to 'reported'
    const result = await db.query(
        `UPDATE users SET status = 'reported' WHERE id = ?`,
        [reportedUserId]
    );

    return result;
}


