const db = require("../db/index");

module.exports = class Admin {
  // Products Section
  insertProduct(data) {
    
    const query =
      `INSERT INTO all_products (category,p_code,title,slug_name,gst,discount, selling_price, display_price, description, is_featured,on_sale,views,qty_sold,shipping_charges,total_qty,thumbnail,multi_img,status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`;
    
    return db.query(query, [
      data.category,
      data.p_code,
      data.title,
      data.title,
      data.gst,
      data.discount,
      data.selling_price,
      data.display_price,
      data.description,
      data.is_featured,
      data.on_sale,
      data.on_sale,
      data.total_qty,
      data.shipping_charges,
      data.total_qty,
      data.thumbnail,
      JSON.stringify(data.multi_img),
      1
    ]);
  }

  // Products Section

  // common Functions
  getData(table, starting_page, product_per_page, where) {
    const query = `SELECT * from ${table} ${where} LIMIT ${starting_page}, ${product_per_page} `;
    console.log(query);
    return db.query(query);
  }

  getUserDetails() {
    const query = `SELECT COUNT(*) total, 
    SUM(LAST_DAY(created_date) = LAST_DAY(CURDATE())) this_month ,
    SUM(date(created_date) = date(CURDATE())) today FROM users`;
    return db.query(query);
  }

  getOrderDetails() {
    const query = `SELECT COUNT(*) total, 
    SUM(LAST_DAY(created_at) = LAST_DAY(CURDATE())) this_month ,
    SUM(date(created_at) = date(CURDATE())) today FROM orders`;
    return db.query(query);
  }

  getCurrentOrderDetails() {
    const query = `SELECT * FROM orders Order by created_at DESC LIMIT 5`;
    return db.query(query);
  }

  getAllRowCount(table) {
    const query = `SELECT id from ${table}`;
    return db.query(query);
  }

  updateStatus(id, column, status, table) {
    const query = `UPDATE ${table} 
    SET ${column} = ${status}
     WHERE id = '${id}'`;
    return db.query(query);
  }

  getCategories(table) {
    let query = `SELECT DISTINCT category FROM ${table}`
    return db.query(query);
  }

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