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

export default {
  getf0cks: async (o = { user, tag, mime, page, mode, fav }) => {
    const user    = o.user ? decodeURI(o.user) : null;
    const tag     = lib.parseTag(o.tag ?? null);
    const mime    = (o.mime ?? "");
    const page    = +(o.page ?? 1);
    const smime   = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
    
    const tmp = { user, tag, mime, smime, page };
  
    const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
  
    let data;
    let total;
      
    if(tag) {
      if(tag.match(/sfw/) || tag.length <= 2)
        return {
          success: false,
          message: "nope."
        };
  
      total = (await sql("items")
        .select("items.id", "items.mime", "st.tag")
        .innerJoin(
          sql("tags")
            .select("tags_assign.item_id", "tags.tag")
            .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
            .where("tags.tag", "ilike", "%"+tag+"%")
            .groupBy("tags_assign.item_id", "tags.tag")
            .as("st"),
          "st.item_id", "items.id"
        )
        .whereRaw(modequery)
        .groupBy('items.id', 'st.tag', 'st.item_id')
        .count("* as total")
      )[0]?.total;
    }
    else {
      if(!o.fav) {
        total = (await sql("items")
          .whereRaw(modequery)
          .andWhere("items.mime", "ilike", smime)
          .andWhere("items.username", "ilike", user ? user : "%")
          .count("* as total")
        )[0]?.total;
      }
      else {
        total = (await sql("favorites")
          .select('items.id', 'items.mime')
          .leftJoin('user', 'user.id', 'favorites.user_id')
          .leftJoin('tags_assign', 'tags_assign.item_id', 'favorites.item_id')
          .leftJoin('tags', 'tags.id', 'tags_assign.tag_id')
          .leftJoin('items', 'items.id', 'favorites.item_id')
          .whereRaw(modequery)
          .andWhere('items.mime', 'ilike', smime)
          .andWhere('user.user', 'ilike', user)
          .groupBy('items.id')
          .count('* as total')
        )[0]?.total;
      }
    }

    if(!total || total.length === 0)
      return {
        success: false,
        message: "404 - no f0cks given"
      };
  
    const pages = +Math.ceil(total / cfg.websrv.eps);
    const act_page = Math.min(pages, page || 1);
    const offset = Math.max(0, (act_page - 1) * cfg.websrv.eps);

    let rows;
  
    if(!o.fav) {
      rows = sql("items")
        .select("items.id", "items.mime", "tags_assign.tag_id")
        .joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)")
        .whereRaw(modequery)
        .andWhere("items.mime", "ilike", smime)
        .andWhere("items.username", "ilike", user ? user : "%")
        .orderBy("items.id", "desc")
        .offset(offset)
        .limit(cfg.websrv.eps);
    }
    else {
      rows = sql("favorites")
        .select('items.id', 'items.mime', 'ta.tag_id')
        .leftJoin('user', 'user.id', 'favorites.user_id')
        .leftJoin('tags_assign', 'tags_assign.item_id', 'favorites.item_id')
        .leftJoin('tags', 'tags.id', 'tags_assign.tag_id')
        .leftJoin('items', 'items.id', 'favorites.item_id')
        .joinRaw("left join tags_assign as ta on ta.item_id = items.id and (ta.tag_id = 1 or ta.tag_id = 2)")
        .whereRaw(modequery)
        .andWhere('items.mime', 'ilike', smime)
        .andWhere('user.user', 'ilike', user)
        .orderBy('items.id', 'desc')
        .groupBy('items.id', 'ta.tag_id')
        .offset(offset)
        .limit(cfg.websrv.eps);
    }
  
    if(tag) {
      rows = rows
        .innerJoin(
          sql("tags")
            .select("tags_assign.item_id", "tags.tag")
            .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
            .where("tags.tag", "ilike", "%"+tag+"%")
            .groupBy("tags_assign.item_id", "tags.tag")
            .as("st"),
          "st.item_id", "items.id"
        )
        .groupBy('st.item_id', 'items.id', 'tags_assign.tag_id');
    }

    
    rows = await rows;
    
      
    
    if(rows.length === 0)
      return {
        success: false,
        message: "oopsi woopsi"
      };
  
    /*rows.forEach(e => {
      if(!fs.existsSync(`public/t/${e.id}.png`))
        fs.copyFileSync("public/s/img/broken.png", `public/t/${e.id}.png`);
    });*/
  
    const cheat = [];
    for(let i = Math.max(1, act_page - 3); i <= Math.min(act_page + 3, pages); i++)
      cheat.push(i);
  
    const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });
  
    data = {
      success: true,
      items: rows,
      pagination: {
        start: 1,
        end: pages,
        prev: (act_page > 1) ? act_page - 1 : null,
        next: (act_page < pages) ? act_page + 1 : null,
        page: act_page,
        cheat: cheat,
        uff: false
      },
      link: link,
      tmp: tmp
    };
    return data;
  },
  getf0ck: async (o = ({ user, tag, mime, itemid, mode })) => {
    const user    = o.user ? decodeURI(o.user) : null;
    const tag     = lib.parseTag(o.tag ?? null);
    const mime    = (o.mime ?? "");
    const itemid  = +(o.itemid ?? 404);
    const smime   = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
    
    const tmp = { user, tag, mime, smime, itemid };
  
    const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
  
    if(itemid === 404) {
      return {
        success: false,
        message: "404 - f0ck not found"
      };
    }
  
    let items;
    
    if(o.fav) {
      items = sql('favorites')
        .select('items.*')
        .leftJoin('items', 'items.id', 'favorites.item_id')
        .leftJoin('user', 'user.id', 'favorites.user_id')
        .whereRaw(modequery)
        .andWhere('user.user', 'ilike', user)
    }
    else {
      items = sql("items")
        .select("*")
        .orderBy("items.id", "desc")
        .whereRaw(modequery);
    }

    if(tag) {
      items = items.innerJoin(
        sql("tags")
          .select("tags_assign.item_id", "tags.tag")
          .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
          .where("tags.tag", "ilike", "%"+tag+"%")
          .groupBy("tags.tag", "tags_assign.item_id")
          .as("st"),
        "st.item_id", "items.id"
      )
      .groupBy('st.tag', 'st.item_id', 'items.id');
    }
    else
      items = items.groupBy('items.id');
    if(user && !o.fav)
      items = items.andWhere("items.username", "ilike", "%" + user + "%");
    if(mime)
      items = items.andWhere("items.mime", "ilike", mime + "/%");
    
    items = await items;

    if(tag)
      items = items.filter((v, i, s) => i === s.findIndex(t => t.id === v.id));
  
    const item = items.findIndex(i => i.id === itemid);
    const actitem = items[item];
  
    if(!actitem) { // sfw-check!
      return {
        success: false,
        message: "Sorry, this post is currently not visible."
      };
    }
  
    const tags = await lib.getTags(itemid);
    const cheat = [...new Set(items.slice(Math.max(0, item - 3), item + 4).map(i => i.id))];
    const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });
    const favorites = await sql('favorites')
      .select('user.user', 'user_options.avatar')
      .leftJoin('user', 'user.id', 'favorites.user_id')
      .leftJoin('user_options', 'user_options.user_id', 'favorites.user_id')
      .where('favorites.item_id', itemid);
    
    let coverart = true;
    try {
      await fs.promises.access(`./public${cfg.websrv.paths.coverarts}/${actitem.id}.webp`);
    } catch(err) {
      coverart = false;
    }
  
    const data = {
      success: true,
      user: {
        name: actitem.username,
        channel: actitem.usernetwork == "Telegram" && actitem.userchannel !== "f0ck.me" ? "anonymous" : actitem.userchannel,
        network: actitem.usernetwork
      },
      item: {
        id: actitem.id,
        src: {
          long: actitem.src,
          short: url.parse(actitem.src).hostname,
        },
        thumbnail: `${cfg.websrv.paths.thumbnails}/${actitem.id}.png`,
        coverart: coverart ? `${cfg.websrv.paths.coverarts}/${actitem.id}.webp` : '/s/img/music.webp',
        dest: `${cfg.websrv.paths.images}/${actitem.dest}`,
        mime: actitem.mime,
        size: lib.formatSize(actitem.size),
        timestamp: {
          timeago: lib.timeAgo(new Date(actitem.stamp * 1e3).toISOString()),
          timefull: new Date(actitem.stamp * 1e3).toISOString()
        },
        favorites: favorites,
        tags: tags
      },
      title: `${actitem.id} - f0ck.me`,
      pagination: {
        end: items[items.length - 1]?.id,
        start: items[0]?.id,
        next: items[item + 1]?.id,
        prev: items[item - 1]?.id,
        page: actitem.id,
        cheat: cheat,
        uff: true
      },
      phrase: cfg.websrv.phrases[~~(Math.random() * cfg.websrv.phrases.length)],
      link: link,
      tmp: tmp
    };
    return data;
  },
  getRandom: async (o = ({ user, tag, mime, mode })) => {
    const user    = o.user ? decodeURI(o.user) : null;
    const tag     = lib.parseTag(o.tag ?? null);
    const mime    = (o.mime ?? "");
    const itemid  = +(o.itemid ?? 404);
    const smime   = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";

    const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);

    let item;
    if(o.fav) {
      item = sql("favorites")
        .select('items.*')
        .leftJoin('items', 'items.id', 'favorites.item_id')
        .leftJoin('user', 'user.id', 'favorites.user_id')
        .whereRaw(modequery)
        .orderByRaw("random()")
        .andWhere('user.user', 'ilike', user);
    }
    else {
      item = sql("items")
        .select("*")
        .orderByRaw("random()")
        .whereRaw(modequery);
    }

    if(tag) {
      item = item.innerJoin(
        sql("tags")
          .select("tags_assign.item_id", "tags.tag")
          .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
          .where("tags.tag", "ilike", "%"+tag+"%")
          .groupBy("tags_assign.item_id", "tags.tag")
          .as("st"),
        "st.item_id", "items.id"
      )
      .groupBy('st.item_id', 'st.tag', 'items.id');
    }
    if(user)
      item = item.andWhere("username", "ilike", "%" + user + "%");
    if(mime)
      item = item.andWhere("mime", "ilike", mime + "/%");
    
    item = await item;

    if(item.length === 0) {
      return {
        success: false,
        message: "no f0cks found :("
      };
    }

    const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });

    return {
      success: true,
      link: link,
      itemid: item[0].id
    };
  }
};
