const { query, nonQuery } = require("../db-config/postgresql-common");

//SELECT own shopping lists
async function getShoppinglistsAdmin(username) {
    try {

        let result = await query('SELECT row_to_json("Shoppinglist") AS obj FROM "Shoppinglist" JOIN "Shoppinglist_admin" USING (sl_id) WHERE \
        username = $1', [username]);

        return result;
    }

    catch (error) {
        console.error(error);
    }
}

//SELECT shared shopping lists

async function getShoppinglistsShared(username) {
    try {
        let result = await query('SELECT row_to_json("Shoppinglist") AS obj FROM "Shoppinglist" JOIN "Shoppinglist_member" USING (sl_id) WHERE \
        username = $1', [username]);
        console.log(result);
        return result;
    }

    catch (error) {
        console.error(error);
    }
}

//INSERT new Shoppinglist

async function newShoppinglist(name, description, username) {

    //generate sl_id

    let sl_id = generate_sl_id();

    //insert user if not exists

    try {
        await nonQuery('INSERT INTO "User" (username) VALUES ($1);', [username]);
    } 
    
    catch (error) {
        console.error(error);
    }

    //insert shoppinglist
    try {
        await nonQuery('INSERT INTO "Shoppinglist" (sl_id, name, description) VALUES ($1, $2, $3);', [sl_id, name, description]);
    } 
    
    catch (error) {
        console.error(error);
    }

    //insert admin
    try {
        await nonQuery('INSERT INTO "Shoppinglist_admin" (username, sl_id) VALUES ($1, $2);', [username, sl_id]);
    }  
    
    catch (error) {
        console.error(error);
    }
}

async function generateUser() {
        //insert user

        try {
            await nonQuery('INSERT INTO "User" (username) VALUES ($1);', [username]);
        } 
        
        catch (error) {
            console.error(error);
        }
}

//get shoppinglist content
async function displayShoppinglist(sl_id) {
    try {
        let groups = await query('SELECT row_to_json("Group") AS obj FROM "Group" JOIN "Shoppinglist" USING (sl_id) WHERE sl_id = $1;', [sl_id]);
        let items = await query('SELECT row_to_json("Item") AS obj FROM "Item" JOIN "Group" USING (group_id) WHERE "Group".sl_id = $1;', [sl_id]);
        return items_in_groups(groups, items);
        
        
    }

    catch (error) {
        console.error(error);
    }
}

//delete shoppinglist

async function deleteShoppinglist(sl_id) {

    try {
        await nonQuery('DELETE FROM "Shoppinglist_admin" WHERE sl_id = $1', [sl_id]);
        await nonQuery('DELETE FROM "Shoppinglist_member" WHERE sl_id = $1', [sl_id]);
        await nonQuery('DELETE FROM "Shoppinglist" WHERE sl_id = $1', [sl_id]);
    } 
    
    catch (error) {
        console.error(error);
    }
}


//sl_id generieren
function generate_sl_id() {
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var output = "";

    for(let i = 0; i < 8; i++) {
        output += possible.charAt(Math.floor(Math.random() * possible.length));
    }

    return output;
}

function items_in_groups(groups, items) {

    let result = [];

    for(let item of groups) {

        result.push({
            group_id: item.group_id,
            name: item.name,
            color: item.color,
            content: items.filter(function(obj) {return obj.group_id == item.group_id})
        });
    }

    return result;
}

module.exports = {
    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist
}
