const v4 = require('uuid/v4');
const pg = require('pg');
const pgescape = require('pg-escape');

/*
  TODO: find a better place for this
  pg table init command (this is totally the right way to do things like this):
  // ---
  CREATE TABLE notes (
    id varchar(8) PRIMARY KEY,
    lastuse timestamp NOT NULL,
    content text NOT NULL
  );
*/

// TODO: pool clients
// pg initialize
let client;

function initDb(){
  return new Promise((resolve, reject) => {
    if (process.env.DATABASE_USE_SSL === true) {
      pg.defaults.ssl = true;
    }
    client = new pg.Client(process.env.DATABASE_URL);
    client.connect(function(err, client) {
      if (err) throw err;
      console.log('client connected to postgres!');
      resolve({success: true});
    });
  })
}



/* Fake db store */
const store = {};
function persist(id, content) {
  return new Promise((resolve, reject) => {
    // For some reason this leads to locks on my local db. 
    // Shimming this pgescape in there for the time being
    // This is really unfortunate
    const qval = pgescape("UPDATE notes SET content = %Q, lastuse = now() WHERE id = %Q", content, id)
    client.query(
      {
        text: qval,
        //values: [id, content],
      },
      (err, result) => {
        if (err) throw err;
        resolve({success: true});
      }
    );
  });
}

function recall(id) {
  return new Promise((resolve, reject) => {
    client.query(
      {
        text: "SELECT content FROM notes WHERE id = $1",
        values: [id],
      },
      (err, result) => {
        if (err) throw err;
        resolve(result.rows[0].content);
      }
    );
  });
}

function exists(id) {
  // this should probably throw on nonexistence (maybe call ensureExists)
  return new Promise((resolve, reject) => {
    client.query(
      {
        text: "select exists(select 1 from notes where id=$1)",
        values: [id],
      },
      (err, result) => {
        if (err) throw err;
        resolve(result.rows[0].exists);
      }
    );
  });
}

function destroy(id) {
  delete store[id];
  return Promise((resolve, reject) => resolve(
    {success: true}
  ));
}

function create() {
  // TODO: make this retry infinitely until a new id is found.
  return new Promise((resolve, reject) => {
    let newId;
    newId = v4().split('-')[0];
    console.log(`Creating note ${newId}`);
    client.query(
      {
        text: "INSERT INTO notes VALUES ($1, now(), '');", // TODO: move empty string default in postgres, not here
        values: [newId],
      },
      (err) => {
        if (err) throw err;
        resolve(newId);
      }
    );
  });
}

// destroys notes that are greater than 30 days old;
function destroyOldNotes() {
  return new Promise((resolve, reject) => {
    console.log('Deleting old notes...');
    client.query(
      {
        text: "DELETE FROM notes WHERE lastuse <= (now() - interval '30 days');"
      },
      (err) => {
        if (err) throw err;
        console.log('Deleted!');
        resolve({success: true});
      }
    );
  });
}

function checkStatus(ids){
  return new Promise((resolve, reject) => {
    client.query(
      {
        text: `SELECT id, content FROM notes WHERE id IN (${ids.map(id => pgescape.literal(id)).join(', ')});`,
      },
      (err, queryResult) => {
        if (err) throw err;
        const statuses = queryResult.rows.map(({id, content}) => {
          let abbreviation = content.split('\n')[0];
          // Lol this is shit:
          const cutoff = 50
          if (abbreviation.length > cutoff) {
            abbreviation = abbreviation.slice(0, cutoff);
          }
          return ({
            id,
            abbreviation,
          });
        });
        resolve(statuses);
      }
    );
  });
}

module.exports = {create, persist, recall, exists, checkStatus, destroy, initDb, destroyOldNotes};

@@ -887,182 +887,44 @@
     
-// For some reason this leads to locks on my local db. %0A    // Shimming this pgescape in there for the time being%0A    // This is really unfortunate%0A    const qval = pgescape(
+client.query(%0A      %7B%0A        text: 
 %22UPD
@@ -947,18 +947,18 @@
 ntent = 
-%25Q
+$2
 , lastus
@@ -982,82 +982,21 @@
 d = 
-%25Q%22, content, id)%0A    client.query(%0A      %7B%0A        text: qval,%0A        //
+$1%22,%0A        
 valu

remove shim for query issue that popped up locally