import db from "../sql.mjs";
import lib from "../lib.mjs";
import cfg from "../config.mjs";

export default async bot => {

  return [{
    name: "f0ckrand",
    call: /^gib f0ck/i,
    active: false,
    f: async e => {
      let args = e.args.slice(1);

      /*let rows = sql("items").select("id", "username", "mime", "size");

      for(let i = 0; i < args.length; i++) {
        if(args[i].charAt(0) === "!")
          rows = rows.where("username", "not ilike", args[i].slice(1));
        else
          rows = rows.where("username", "ilike", args[i]);
      }

      rows = await rows.orderByRaw("random()").limit(1);*/

      const rows = await db`
        select id, mime, username, size
        from "items"
        where
        ${ args.map(a => a.charAt(0) === "!"
          ? db`username not ilike ${a.slice(1)}`
          : db`username ilike ${a}`
        ).join(' and ')}
        order by random()
      `;

      if(rows.length === 0)
        return await e.reply("nothing found, f0cker");

      return await e.reply(`f0ckrnd: ${cfg.main.url.full}/${rows[0].id} by: ${rows[0].username} (${rows[0].mime}, ~${lib.formatSize(rows[0].size)})`);
    }
  }];
};
