/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.*/

class canopyAdminUtils{
    constructor(){

    } 

    //Methods
    async setPermission(permMap){
        var response = await fetch(`/api/admin/permissions`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({permissionsMap: Object.fromEntries(permMap)})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async setChannelOverride(permMap){
        var response = await fetch(`/api/admin/permissions`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({channelPermissionsMap: Object.fromEntries(permMap)})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async setUserRank(user, rank){
        var response = await fetch(`/api/admin/changeRank`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({user, rank})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async getBans(){
        var response = await fetch(`/api/admin/ban`,{
            method: "GET"
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async banUser(user, permanent, ipBan, expirationDays){
        var response = await fetch(`/api/admin/ban`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({user, permanent, ipBan, expirationDays})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async unbanUser(user){
        var response = await fetch(`/api/admin/ban`,{
            method: "DELETE",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({user})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async getTokeCommands(){
        var response = await fetch(`/api/admin/tokeCommands`,{
            method: "GET"
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async addTokeCommand(command){
        var response = await fetch(`/api/admin/tokeCommands`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({command})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async deleteTokeCommand(command){
        var response = await fetch(`/api/admin/tokeCommands`,{
            method: "DELETE",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({command})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async getEmotes(){
        var response = await fetch(`/api/admin/emote`,{
            method: "GET"
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async addEmote(name, link){
        var response = await fetch(`/api/admin/emote`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({name, link})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async deleteEmote(name){
        var response = await fetch(`/api/admin/emote`,{
            method: "DELETE",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({name})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

    async genPasswordResetLink(user){
       var response = await fetch(`/api/admin/genPasswordReset`,{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "x-csrf-token": utils.ajax.getCSRFToken()
            },
            //Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
            body: JSON.stringify({user})
        });

        if(response.ok){
            return await response.json();
        }else{
            utils.ux.displayResponseError(await response.json());
        }
    }

}

const adminUtil = new canopyAdminUtils();