import sql from "../src/inc/sql.mjs";
import cfg from "../src/inc/config.mjs";
import fetch from "flumm-fetch-cookies";

(async () => {
  const _args = process.argv.slice(2);
  const _from = +_args[0];
  const _to = _from + 100;

  const f0cks = await sql("items")
    .whereRaw("id not in (select item_id from tags_assign group by item_id)")
    .andWhere("mime", "like", "image/%")
    .andWhereBetween("id", [ _from, _to ]);

  const body = { headers: { Authorization: `Basic ${cfg.tagger.btoa}` } };
  
  for(let f of f0cks) {
    let tag;
    const res = await (await fetch(`${cfg.tagger.endpoint}/categories/nsfw_beta?image_url=${cfg.main.url}/b/${f.dest}`, body)).json();
    if(res?.result) {
      tag = (res.result.categories[0].name.en === "safe") ? "sfw" : "nsfw";

      await sql("tags_assign").insert({
        tag_id: tag === "sfw" ? 1 : 2,
        item_id: f.id,
        user_id: 7 // user: autotagger
      });
    }
    else {
      console.log(res);
    }
  };
})();