const db = require('../db/index')

module.exports = class User {

    validateLogin(mobile) {
        const query = "SELECT * from users where mobile = ?";
        return db.query(query, [mobile]);
    }

    register(data) {
        const query = "INSERT INTO users (fullname,mobile,email,password,created_date,status) VALUES (?,?,?,?,?,?)"
        return db.query(query, [data.fullname, data.mobile, data.email, data.password, new Date(), 1])
    }

    checkUserExistsByID(id) {
        const query = "SELECT * from users where id = ?";
        return db.query(query, [id])
    }

    checkUserExists(mobile) {
        const query = "SELECT * from users where mobile = ?";
        return db.query(query, [mobile])
    }

    contact(data) {
        const query = "INSERT INTO support (name,email,mobile,product,message,created_at) VALUES (?,?,?,?,?,?)"
        return db.query(query, [data.name, data.email, data.mobile, data.product, data.message, new Date()])
    }

    updateProfile(data, user_id) {
        const query = "UPDATE users set fullname = ?, email = ?, dob = ?, gender = ? WHERE id = ?"
        return db.query(query, [data.name, data.email, data.dob, data.gender, user_id])
    }

    changeExistingPassword(password, user_id) {
        const query = "UPDATE users set password = ? WHERE id = ?"
        return db.query(query, [password, user_id])
    }

    addAddress(data, user_id) {
        const query = "INSERT INTO user_address (user_id,type,name,mobile,address,city,state,pincode,created_at,updated_at,status) VALUES (?,?,?,?,?,?,?,?,?,?,?)"
        return db.query(query, [user_id, data.type, data.name, data.mobile, data.address, data.city, data.state, data.pincode, new Date(), null, 1])
    }

    updateAddress(data) {
        const query = "UPDATE user_address set type = ?,name = ? ,mobile = ?,address = ?,city = ?,state = ?,pincode = ?,updated_at = ? WHERE id = ?"
        return db.query(query, [data.type, data.name, data.mobile, data.address, data.city, data.state, data.pincode, new Date(), data.id])
    }

    address(user_id) {
        const query = "SELECT * from user_address where user_id = ? and status = ?";
        return db.query(query, [user_id, 1])
    }

    deleteAddress(id) {
        const query = "UPDATE user_address set status = 0 WHERE id = ?";
        return db.query(query, [id])
    }

    addressByID(id) {
        const query = "SELECT * from user_address where id = ?";
        return db.query(query, [id])
    }

    saveVerificationCode(mobile, verificationCode) {
        const query = "UPDATE users set verification_code = ? WHERE mobile = ?";
        return db.query(query, [verificationCode, mobile])
    }

    checkVerificationCode(mobile, verificationCode) {
        const query = "SELECT mobile, verification_code from users where mobile = ? AND verification_code = ?";
        return db.query(query, [mobile, verificationCode])
    }

    updatePassword(mobile, verificationCode, password) {
        const query = "UPDATE users set password = ? WHERE mobile = ? AND verification_code = ?";
        return db.query(query, [password, mobile, verificationCode])
    }

    ourProducts() {
        const query = "SELECT * from all_products WHERE status = 1 limit 8";
        return db.query(query)
    }

    bestSellerProducts() {
        const query = "SELECT * from all_products WHERE status = 1 limit 4";
        return db.query(query)
    }

    getAllProductsByCategory(category) {
        const query = "SELECT * from all_products WHERE category = ? and status = 1";
        return db.query(query, [category])
    }

    getProductByPcode(p_code) {
        const query = "SELECT * from all_products WHERE p_code = ? AND status = 1";
        return db.query(query, [p_code])
    }

    getRelatedProducts() {
        const query = "SELECT * from all_products WHERE status = 1 ORDER BY RAND() LIMIT 4";
        return db.query(query)
    }

    getProductByID(id) {
        const query = "SELECT * from all_products WHERE id = ? AND status = 1";
        return db.query(query, [id])
    }

    getProductByIDCheckout(id) {
        const query = "SELECT id,p_code,title,gst,discount,shipping_charges,selling_price,display_price,thumbnail from all_products WHERE id = ? AND status = 1";
        return db.query(query, [id])
    }

    placeOrder(data) {
        const query = "INSERT INTO orders (razorpay_order_id,order_id,user_id,details,shipping_charges,gst,processing_fee,final_price,created_at,status) VALUES (?,?,?,?,?,?,?,?,?,?)";
        return db.query(query, [data.razorpay_order_id, data.order_id, data.user_id, data.details, data.shipping_charges, data.gst, data.processing_fee, data.final_price, data.created_at, data.status])
    }

    getOrderByID(order_id, user_id) {
        const query = "SELECT * from orders WHERE order_id = ? and user_id = ?";
        return db.query(query, [order_id, user_id])
    }

    checkOrderID(order_id) {
        const query = "SELECT order_id from orders WHERE order_id = ?";
        return db.query(query, [order_id])
    }

    getOrdersByUserID(user_id) {
        const query = "SELECT * from orders WHERE user_id = ? ORDER BY id DESC";
        return db.query(query, [user_id])
    }

    invoice(order_id, user_id) {
        const query = "SELECT * from orders WHERE order_id = ? and user_id = ?";
        return db.query(query, [order_id, user_id])
    }

    trackOrder(order_id, user_id) {
        const query = "SELECT awb from orders WHERE order_id = ? and user_id = ?";
        return db.query(query, [order_id, user_id])
    }

    log(type, description, ip) {
        const query = "INSERT INTO systemlog (type,log,ip) VALUES (?,?,?)"
        return db.query(query, [type, description, ip])
    }
}