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

//NPM Imports
const {mongoose} = require('mongoose');

//Local Imports
const config = require('./../../config.json');

const statSchema = new mongoose.Schema({
    //This does NOT handle deleted accounts/channels. Use userModel.estimatedDocumentCount() for number of active users.
    userCount: {
        type: mongoose.SchemaTypes.Number,
        required: true,
        default: 0
    },
    channelCount: {
        type: mongoose.SchemaTypes.Number,
        required: true,
        default: 0
    },
    launchCount: {
        type: mongoose.SchemaTypes.Number,
        required: true,
        default: 0
    },
    firstLaunch: {
        type: mongoose.SchemaTypes.Date,
        required: true,
        default: new Date()
    },
    tokes: [{
        toke: {
            type: mongoose.SchemaTypes.Map,
            required: true,
            default: new Map()
        },
        date: {
            type: mongoose.SchemaTypes.Date,
            required: true,
            default: new Date()
        }
    }]
});

//statics
statSchema.statics.getStats = async function(){
    //Get the first document we find
    var stats = await this.findOne({});
    
    if(stats){
        //If we found something then the statistics document exist and this is it,
        //So long as no one else has fucked with the database it should be the only one. (is this forshadowing for a future bug?)
        return stats;
    }else{
        //Otherwise this is the first launch of the install, say hello
        console.log("First launch detected! Initializing statistics document in Database!");

        //create and save the statistics document
        stats = await this.create({});
        await stats.save();

        //live up to the name of the function
        return stats;
    }
}

statSchema.statics.incrementLaunchCount = async function(){
    //get our statistics document
    const stats = await this.getStats();

    //increment counter and save
    stats.launchCount++;
    stats.save();

    //print bootup message to console.
    console.log(`${config.instanceName}(Powered by Canopy) initialized. This server has booted ${stats.launchCount} time${stats.launchCount == 1 ? '' : 's'}.`)
    console.log(`First booted on ${stats.firstLaunch}.`);
}

statSchema.statics.incrementUserCount = async function(){
    //get our statistics document
    const stats = await this.getStats();
    //temporarily keep old count so we can return it for the users ID 
    const oldCount = stats.userCount;

    //increment counter and save
    stats.userCount++;
    stats.save();

    //return the count from beggining of function for user ID
    return oldCount;
}

statSchema.statics.incrementChannelCount = async function(){
    //get our statistics document
    const stats = await this.getStats();
    //temporarily keep old count so we can return it for the channel ID 
    const oldCount = stats.channelCount;

    //increment counter and save
    stats.channelCount++;
    stats.save();

    //return the count from beggining of function for channel ID
    return oldCount;
}

statSchema.statics.tattooToke = async function(toke){
    //Get the statistics document
    const stats = await this.getStats();

    //Add the toke to the stat document
    stats.tokes.push({toke});

    //Save the stat document
    await stats.save();
}

statSchema.statics.getTokeCount = async function(){
    //get stats doc
    const stats = await this.getStats();

    //return toke count
    return stats.tokes.length;
}

statSchema.statics.getTokeCommandCounts = async function(){
    //get stats doc
    const stats = await this.getStats()
    //Create empty map to hold toke command counts
    const count = new Map();

    //for each toke
    stats.tokes.forEach((toke) => {
        //For each toke command called in the current toke
        toke.toke.forEach((command) => {
            //Get the current count for the current command
            var curCount = count.get(command);

            //if the current count is null
            if(curCount == null){
                //Set it to one
                count.set(command, 1);
            }else{
                //Set it to ++curCount
                count.set(command, ++curCount);
            }
        });
    });

    //return the toke command count
    return count;
}

module.exports = mongoose.model("statistics", statSchema);