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

const regex = /(https\:\/\/f0ck\.me|http\:\/\/fockmoonsb24iczs7odozzy5uktlzbcgp337nabrgffzxv5ihabgpvyd\.onion)(\/(video|image|audio))?\/(\d+|(?:b\/)(\w{8})\.(jpg|webm|gif|mp4|png|mov|mp3|ogg|flac))/gi;

export default async bot => {

  return [{
    name: "f0ckgag",
    call: regex,
    active: true,
    f: async e => {
      const dat = e.message.match(regex)[0].split(/\//).pop();
      let query = sql("items").select("id", "mime", "size", "username", "userchannel", "usernetwork", "stamp");

      if(dat.includes("."))
        query = query.where("dest", "like", `%${dat}%`);
      else
        query = query.where("id", dat);
      
      const rows = await query;
      if(rows.length === 0)
        return e.reply("no f0cks given! lol D:");
      
      e.reply([
        `${cfg.main.url}/${rows[0].id}`,
        `user: ${rows[0].username} @ ${rows[0].usernetwork} ${rows[0].userchannel}`,
        `~${lib.formatSize(rows[0].size)}`,
        rows[0].mime,
        new Date(rows[0].stamp * 1e3).toString().slice(0, 24)
      ].join(" - "));
    }
  }];
};
