/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 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/>.*/

//local includes
const auxServer = require("../../auxServer");
const socketUtils = require("../../../utils/socketUtils")
const loggerUtils = require("../../../utils/loggerUtils");
const channelModel = require("../../../schemas/channel/channelSchema");

/**
 * Class containg global server-side private message relay logic
 * 
 * Exists to make broadcasting channel queues to groups of authenticated users with the 'read-queue' perm as painless as possible,
 * reducing DB call/perm checks to just connection time, and not requireing any out-of-library user iteration at broadcast time.
 * 
 * Calls to modify and write to the schedule are still handled by the main namespace
 * This is both for it's ease of access to the rest of the channel logic, but also to keep this class as small as possible.
 */
class queueBroadcastManager extends auxServer{
    /**
     * Instantiates object containing global server-side channel schedule broadcasting subsystem
     * @param {Socket.io} io - Socket.io server instanced passed down from server.js 
     * @param {channelManager} chanServer - Sister channel management server object
     */
    constructor(io, chanServer){
        super(io, chanServer, "/queue-broadcast");
    }

    /**
     * Handles global server-side initialization for new connections to the queue broadcast subsystem
     * @param {Socket} socket - Requesting Socket
     */
    async handleConnection(socket){
        //Check if we're properly authorized
        const userObj = await super.handleConnection(socket);

        //If we're un-authorized
        if(userObj == null){
            //Drop the connection
            return;
        }

        //Set socket channel value
        socket.chan = socketUtils.getChannelName(socket);
        //Pull active channel
        const activeChannel = this.chanServer.activeChannels.get(socket.chan);

        //If there isn't an active channel
        if(activeChannel == null){
            //Drop the connection
            return;
        }

        //Pull channel DB
        const chanDB = (await channelModel.findOne({name: socket.chan}));

        //If the user is connecting from an invalid channel
        if(chanDB == null){
            //Drop the connection
            return;
        }

        //If the user is allowed to read the schedule
        if(await chanDB.permCheck(socket.user, 'readSchedule')){
            //Throw the user into the channels room within the queue-broadcast instance
            socket.join(socket.chan);

            //Send the queue down to our newly connected user
            activeChannel.queue.emitQueue(chanDB, socket);

            //Define listeners
            this.defineListeners(socket);
        }
    }

    defineListeners(socket){
        super.defineListeners(socket);
    }
}

module.exports = queueBroadcastManager;