'use strict';
var slug = require('slug');
var db = require('./db.js');

module.exports = exports = Note;

/**
 * Represents a note instace.
 */
function Note(details) {
  if (!(this instanceof Note)) {
    return new Note(note);
  }

  this.note = details['note'];
  this.complete = details['complete'] || false;
  this.slug = details['slug'] || slug(this.note);
  this._id = this.constructor.setId(this.slug);
}

Note._key = db._makeKey(Note);
Note.all = all;
Note.create = create;
Note.remove = remove;
Note.setId = setId;
Note.prototype.remove = removeInstance;

/**
 * Convert a note instance into a JSON string.
 * @param {Note} note: instance to be stringified
 * @return {String} the instance as a JSON string.
 */
function toJSON(note) {
    return JSON.stringify({
      "note": note.note,
      "complete": note.complete,
      "slug": note.slug
    });
}

function toNote(details) {
  console.log(details);
  console.log(JSON.parse(details));
  return new Note(JSON.parse(details));
}

/**
 * Retrieve all notes available in the database
 *
 * @return {Stream}: a readable stream with objectMode set to true
 */
function all() {
  return db.createRpcStream();
}

/**
 * Create a new note.
 *
 * @param {Object} details: an object with the note details, the following keys
 * are expected:
 *     * {String} note
 *     * {Boolean} complete
 *     * {String} slug [optional]
 * @param {Function} fn: callback to be executed after the note is persisted,
 * receives an error || null and the note instance.
 */
function create(details, fn) {
  var note = new this(details);
  db.put(
    note._id,
    toJSON(note),
    function (err) {
      fn(err, note);
    }
  );
}

function remove(slug, fn) {
  var _id = this.setId(slug);
  console.log('id', _id);
  db.get(_id, function (err, data) {
    var details = toNote(data);
    db.del(_id, function (err) {
      console.log(err);
      fn(err, details);
    });
  });
}

/**
 * Change the complete status of a note instance.
 *
 * @param {Function} fn: callback to be execute after the note is persisted,
 * receives an error || null and the note instance.
 */
function change(fn) {
  var self = this;
  this.complete = !this.complete;
  db.put(
    this._id,
    toJSON(this),
    function (err) {
      fn(err, self);
    }
  );

}

/**
 * Remove a note instance.
 *
 * @param {Function} fn: callback to be execute after the note is removed,
 * receives an error || null and the note instance.
 */
function removeInstance(fn) {
  var self = this;
  db.del(
    this._id,
    function (err) {
      fn(err, self);
    }
  );
}

/**
 * Define the id attribute for a given instance.
 */
function setId(slug) {
  return this._key + slug;
}

@@ -659,16 +659,17 @@
 ingified
+.
 %0A * @ret
@@ -865,82 +865,195 @@
 %0A%7D%0A%0A
-function toNote(details) %7B%0A  console.log(details);%0A  console.log(JSON.pars
+/**%0A * Convert a string of details into a Note instance.%0A * @param %7BString%7D details: a string representation of a json.%0A * @return %7BNote%7D the string as a Note instance.%0A */%0Afunction toNot
 e(de
@@ -1058,18 +1058,18 @@
 details)
-);
+ %7B
 %0A  retur
@@ -1787,16 +1787,241 @@
   );%0A%7D%0A%0A
+/**%0A * Remove the given slug from the database%0A * @param %7BString%7D slug: slug part of note id.%0A * @param %7BFunction%7D fn: callback to be execute after the note is removed,%0A * receives an error %7C%7C null and the note instance.%0A */%0A
 function
@@ -2074,34 +2074,8 @@
 g);%0A
-  console.log('id', _id);%0A
   db
@@ -2176,32 +2176,8 @@
 ) %7B%0A
-      console.log(err);%0A
     

Remove `console#log` calls and add documentation to models.