/*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/>.*/

//NPM Imports
const { checkSchema } = require('express-validator');
const validator = require('validator');//We need validators for express-less code too!
const { errorMiddleware } = require('../utils/loggerUtils');

module.exports = {

}

module.exports.name = function(field = 'name'){
    return checkSchema({
        [field]: {
            escape: true,
            trim: true,
            isAlphanumeric: {
                errorMessage: "Emote names must be alphanumeric."
            },
            isLength: {
                options: {
                    min: 1,
                    max: 14
                },
                errorMessage: "Emote name must be between 1 and 14 characters."
            },
        }
    })
}

module.exports.link = function(field = 'link'){
    return checkSchema({
        [field]: {
            isURL: {
                options: {
                    require_tld: false,
                    require_host: false,
                    require_valid_protocol: true
                },
                errorMessage: "Invalid URL."
            },
            trim: true
        }
    })
}

module.exports.manualName = function(input){
    //Trim and sanatize input
    const clean = validator.trim(validator.escape(input));

    //if cleaned input is a proper emote name
    if(validator.isLength(clean, {min: 1, max: 14}) && validator.isAlphanumeric(clean)){
        //return cleaned input
        return clean;
    }

    //otherwise return false
    return false;
}

module.exports.manualLink = function(input){
    //Trim the input
    const clean = validator.trim(input)

    //If we have a URL return the trimmed input
    if(validator.isURL(clean,{require_valid_protocol: true})){
        return clean;
    }

    //otherwise return false
    return false;
}