import cfg from "../config.mjs";
import db 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();
      const rows = await db`
        select id, mime, size, username, userchannel, usernetwork, stamp
        from "items"
        ${ dat.includes('.')
          ? db`where dest ilike ${'%' + dat + '%'}`
          : db`where id = ${dat}`
        }
      `;

      if(rows.length === 0)
        return await e.reply("no f0cks given! lol D:");
      
      await e.reply([
        `${cfg.main.url.full}/${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(" - "));
    }
  }];
};
