var fs = require('fs-extra');

module.exports = (lib) => {
  lib.trigger.add({
    name: 'del',
    call: /^\!(del|rm)/i,
    level: 100,
    active: 0,
    func: (e) => {
      var ids = e.message.split(' ');
      ids.shift();
      ids.forEach(id => {
        if(Number.isInteger(parseInt(id)) && parseInt(id) > 0) {
          lib.sql.query("select `dest` from `f0ck`.`items` where `id` = ? limit 1", id, (err, rows) => {
            if(rows[0]) {
              lib.sql.query("delete from `f0ck`.`items` where `id` = ? limit 1", id, err => {
                if(!err) {
                  fs.unlink(rows[0].dest, err => {});
                  fs.unlink("./t/"+id+".png", err => {});
                }
                else
                  e.reply(err);
              });
            }
          });
        }
      });
      e.reply("f0ck"+(ids.length>1?"s":"")+" deleted");
    },
    desc: 'delete f0ck'
  });
};
