import db 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 db`
        select count(*) as total
        from items
        inner join (
          select tags_assign.item_id, tags.tag
          from tags
          left join tags_assign on tags_assign.tag_id = tags.id
          where tags.tag ilike ${'%' + (tag ? tag : '') + '%'}
          group by tags_assign.item_id, tags.tag
        ) as st on st.item_id = items.id
        where ${db.unsafe(modequery)}
        group by st.tag, st.item_id`;

      total = total?.length;
    }
    else {
      if(!o.fav) {
        total = await db`
          select count(*) as total
          from items
          where ${db.unsafe(modequery)}
            and items.mime ilike ${smime}
            and items.username ilike ${user ? user : '%'}
        `;
        total = total[0].total;
      }
      else {
        total = await db`
          select count(*) as total
          from "favorites"
          left join "user" on "user".id = "favorites".user_id
          left join "tags_assign" on "tags_assign".item_id = "favorites".item_id
          left join "tags" on "tags".id = "tags_assign".tag_id
          left join "items" on "items".id = "favorites".item_id
          where ${db.unsafe(modequery)}
            and "items".mime ilike ${smime}
            and "user".user ilike ${user}
          group by "items".id
        `;
        total = 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 = db`
        select "items".id, "items".mime, "tags_assign".tag_id
        from "items"
        left join "tags_assign" on "tags_assign".item_id = "items".id and ("tags_assign".tag_id = 1 or "tags_assign".tag_id = 2)
        ${tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag
            ) as st on st.item_id = "items".id
            `
          : db``
        }
        where ${db.unsafe(modequery)}
          and "items".mime ilike ${smime}
          and "items".username ilike ${user ? user : '%'}
        ${tag
          ? db`group by st.item_id, "items".id, "tags_assign".tag_id`
          : db``
        }
        order by "items".id desc
        offset ${offset}
        limit ${cfg.websrv.eps}
      `;
    }
    else {
      rows = db`
        select "items".id, "items".mime, ta.tag_id
        from "favorites"
        left join "user" on "user".id = "favorites".user_id
        left join "tags_assign" on "tags_assign".item_id = "favorites".item_id
        left join "tags" on "tags".id = "tags_assign".tag_id
        left join "items" on "items".id = "favorites".item_id
        left join "tags_assign" as ta on ta.item_id = "items".id and (ta.tag_id = 1 or ta.tag_id = 2)
        ${ tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag
            ) as st on st.item_id = "items".id`
          : db``
        }
        where ${db.unsafe(modequery)}
          and "items".mime ilike ${smime}
          and "user".user ilike ${user}
        ${tag
          ? db`group by st.item_id, "items".id, "tags_assign".tag_id`
          : db``
        }
        group by "items".id, ta.tag_id
        order by "items".id desc
        offset ${offset}
        limit ${cfg.websrv.eps}
      `;
    }

    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', path: 'p/' });
  
    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
      },
      link,
      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 = db`
        select "items".*
        from "favorites"
        left join "items" on "items".id = "favorites".item_id
        left join "user" on "user".id = "favorites".user_id
        ${ tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag
            ) as st on st.item_id = "items".id`
          : db``
        }
        where ${db.unsafe(modequery)}
        and "user".user ilike ${user}
        ${ mime
          ? db`and "items".mime ilike ${mime + '/%'}`
          : db``
        }
        ${ tag
          ? db`group by st.tag, st.item_id, "items".id`
          : db`group by "items".id, "favorites".user_id, "favorites".item_id, "user".id`
        }
        order by "items".id desc
      `;
    }
    else {
      items = db`
        select "items".*
        from "items"
        ${ tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags_assign".tag_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag, "tags_assign".tag_id
            ) as st on st.item_id = "items".id`
          : db``
        }
        where ${db.unsafe(modequery)}
        ${ user
          ? db`and "items".username ilike ${'%' + user + '%'}`
          : db``
        }
        ${ mime
          ? db`and "items".mime ilike ${mime + '/%'}`
          : db``
        }
        ${ tag
          ? db`group by st.item_id, "items".id, st.tag_id`
          : db`group by "items".id`
        }
        order by "items".id desc
      `;
    }

    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', path: '' });
    const favorites = await db`
      select "user".user, "user_options".avatar
      from "favorites"
      left join "user" on "user".id = "favorites".user_id
      left join "user_options" on "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
      },
      phrase: cfg.websrv.phrases[~~(Math.random() * cfg.websrv.phrases.length)],
      link,
      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 modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);

    let item;
    if(o.fav) { // dood lol
      item = db`
        select "items".*
        from "favorites"
        left join "items" on "items".id = "favorites".item_id
        left join "user" on "user".id = "favorites".user_id
        ${ tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag
            ) as st on st.item_id = "items".id`
          : db``
        }
        where ${db.unsafe(modequery)}
        and "user".user ilike ${user}
        ${ mime
          ? db`and "items".mime ilike ${mime + '/%'}`
          : db``
        }
        order by random()
        limit 1
      `;
    }
    else {
      item = db`
        select *
        from "items"
        ${ tag
          ? db`
            inner join (
              select "tags_assign".item_id, "tags".tag
              from "tags"
              left join "tags_assign" on "tags_assign".tag_id = "tags".id
              where "tags".tag ilike ${'%' + tag + '%'}
              group by "tags_assign".item_id, "tags".tag
            ) as st on st.item_id = "items".id`
          : db``
        }
        where ${db.unsafe(modequery)}
        ${ user
          ? db`and "items".username ilike ${'%' + user + '%'}`
          : db``
        }
        ${ mime
          ? db`and "items".mime ilike ${mime + '/%'}`
          : db``
        }
        order by random()
        limit 1
      `;
    }

    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
    };
  }
};