text: const sql = require("../database/db");
  const { pool } = require('../../config/db.config')
  
  const Webhook = function(data) {
    this.ordernumber = data.data.ordernumber;
    this.orderdate = data.orderdate;
    this.data = JSON.stringify(data.data);
    this.sended = data.sended;
    this.createdat = data.createdat;
  };
  
  Webhook.create = async (newWebhook, result) => {
    console.log('Webhook.create');
    const query ='INSERT INTO webhook (ordernumber, orderdate, data, sended, createdat) VALUES ($1, $2, $3, $4, $5)';
    pool.query(query, [newWebhook.ordernumber, newWebhook.orderdate, newWebhook.data, newWebhook.sended, newWebhook.createdat])
      .then((res) => { 
        console.log('Webhook.create.then');
        return result(null, { id: res.rowCount, ...newWebhook });
      })
      .catch((err) => {
        console.log('Webhook.create.catch');
        if (error) {
          console.log('error: ', error)
          return result(error, null);
        }
      });
  };
  
  Webhook.findById = (id, result) => {
    pool.query(`SELECT * FROM webhook WHERE id = ${id}`, (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(err, null);
        return;
      }
  
      if (res.length) {
        console.log("found webhook: ", res[0]);
        result(null, res[0]);
        return;
      }
  
      // not found Tutorial with the id
      result({ kind: "not_found" }, null);
    });
  };
  
  Webhook.getAll = (result) => {
    let query = "SELECT * FROM webhook";
  
    pool.query(query, (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(null, err);
        return;
      }
  
      console.log("webhooks: ", res);
      result(null, res);
    });
  };
  
  // Webhook.findByOrderDate = (orderDate, result) => {
  //   console.log('findByOrderDate');
  //   console.log('orderDate: ', orderDate);
  //   const query = `SELECT * FROM webhook WHERE orderdate >= '${orderDate}'`;
  //   console.log('query: ', query);
  //   pool.query(query, (err, res) => {
  //     if (err) {
  //       console.log("error: ", err);
  //       // result(err, null);
  //       return result(err, null);
  //     }
  
  //     // console.log("res.rows: ", res);
  //     if (res.rowCount >= 1) {
  //       // console.log("found webhook: ", res.rows[0]);
  //       // result(null, res[0]);
  //       return result(null, res.rows);
  //     }
  
  //     // not found Tutorial with the id
  //     // result({ kind: "not_found" }, null);
  //     result(null, null);
  //   })
  // };
  
  Webhook.findByOrderDate = (orderDate, result) => {
    console.log('findByOrderDate');
    console.log('orderDate: ', orderDate);
    const query = `SELECT * FROM webhook WHERE orderdate >= '${orderDate}'`;
    console.log('query: ', query);
    
    pool.query(query).then((res) => {
      console.log('then');
      if (res.rowCount >= 1) {
        return result(null, res.rows);
      }
    }).catch((err) => {
      console.log('catch');
      if (err) {
        console.log("error: ", err);
        // result(err, null);
        return result(err, null);
      }
    });
  };
  
  Webhook.findByOrderNumberNotSended = (orderNumber, result) => {
    console.log('findByOrderNumberNotSended');
    const query = `SELECT * FROM webhook WHERE ordernumber = '${orderNumber}' AND sended = false`;
    console.log('query: ', query);
    pool.query(query, (err, res) => {
      if (err) {
        console.log("error: ", err);
        // result(err, null);
        return result(err, null);
      }
  
      // console.log("res.rowCount: ", res.rowCount);
      if (res.rowCount >= 1) {
        // console.log("found webhook: ", res.rows[0]);
        // result(null, res[0]);
        return result(null, res.rows);
      }
  
      // not found Tutorial with the id
      // result({ kind: "not_found" }, null);
      result(null, null);
    })
  };
  
  Webhook.findByNotSended = (result) => {
    console.log('findByNotSended');
    const query = `SELECT * FROM webhook WHERE sended = false`;
    console.log('query: ', query);
    pool.query(query, (err, res) => {
      if (err) {
        console.log("error: ", err);
        // result(err, null);
        return result(err, null);
      }
  
      console.log("res.rows: ", res.rowCount);
      if (res.rowCount >= 1) {
        // console.log("found webhook: ", res.rows[0]);
        // result(null, res[0]);
        return result(null, res.rows);
      }
  
      // not found Tutorial with the id
      // result({ kind: "not_found" }, null);
      result(null, null);
    })
  };
  
  Webhook.updateById = (id, data, result) => {
    pool.query(
      "UPDATE webhook SET data = ?, sended = ? ? WHERE id = ?",
      [webhook.data, webhook.sended, id],
      (err, res) => {
        if (err) {
          console.log("error: ", err);
          result(null, err);
          return;
        }
  
        if (res.affectedRows == 0) {
          // not found webhook with the id
          result({ kind: "not_found" }, null);
          return;
        }
  
        console.log("updated webhook: ", { id: id, ...webhook });
        result(null, { id: id, ...webhook });
      }
    );
  };
  
  Webhook.remove = (id, result) => {
    pool.query("DELETE FROM webhook WHERE id = ?", id, (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(null, err);
        return;
      }
  
      if (res.affectedRows == 0) {
        // not found Tutorial with the id
        result({ kind: "not_found" }, null);
        return;
      }
  
      console.log("deleted webhook with id: ", id);
      result(null, res);
    });
  };
  
  Webhook.removeAll = result => {
    pool.query("DELETE FROM webhook", (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(null, err);
        return;
      }
  
      console.log(`deleted ${res.affectedRows} webhooks`);
      result(null, res);
    });
  };
  
  module.exports = Webhook;