let bcrypt = require('bcryptjs')
let randomColor = require('randomcolor')
var passportLocalSequelize = require('passport-local-sequelize');
let pagination = require('../lib/pagination.js')
const Errors = require('../lib/errors.js')
var crypto = require("crypto");
var cryptoRandomString = require("crypto-random-string");
module.exports = (sequelize, DataTypes) => {
    let Team = sequelize.define('Team', {
        username: {
            type: DataTypes.STRING(191),
            unique: {
                msg: 'Username is already taken, try another!',
                fields: ['username']
            },
            validate: {
                is: {
                    args: [/^[a-zA-Z0-9_]*$/],
                    msg: 'Username can only contain numbers and letters'
                },
                len: {
                    args: [3, 16],
                    msg: 'username must be between 3 and 16 characters'
                },
                isString (val) {
                    if(typeof val !== 'string') {
                        throw new sequelize.ValidationError('username must be a string')
                    }
                },
                containsNoBlankCharacters (val) {
                    if(/\s/g.test(val)) {
                        throw new sequelize.ValidationError('username can\'t contain blank characters')
                    }
                }
            }
        },
        name: {
            type: DataTypes.STRING,
            validate: {
                isString (val) {
                    if(typeof val !== 'string') {
                        throw new sequelize.ValidationError('Team name must be a string')
                    }
                },
                len: {
                    args: [3, 30],
                    msg: 'Team name must be between 3 and 30 characters'
                }
            }
        },
        description: {
            type: DataTypes.TEXT,
            validate: {
                isString (val) {
                    if(typeof val !== 'string') {
                        throw new sequelize.ValidationError('description must be a string')
                    }
                },
                len: {
                    args: [0, 256],
                    msg: 'Team description must be less than 256 characters'
                }
            }
        },
        banned: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
            isBoolean: {
                msg: "Can only be a true or false value"
            }
        },
        banReason: {
            type: DataTypes.TEXT,
            defaultValue: "No reason provided"
        },
        itemsOptOut: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
            isBoolean: {
                msg: "Can only be a true or false value"
            }
        },
        forumEnabled: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
            isBoolean: {
                msg: "Can only be a true or false value"
            }
        },
        teamWallOptOut: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        TeamRoleId: {
            type: DataTypes.BIGINT,
            default: 0
        },
        approved: {
            type: DataTypes.BOOLEAN,
            default: false,
            defaultValue: false,
            isBoolean: {
                msg: "Can only be a true or false value"
            }
        },
        OwnerId: {
            type: DataTypes.BIGINT,
            default: 1,
            defaultValue: 1,
        },
        inviteOnly: {
            type: DataTypes.BOOLEAN,
            default: false,
            defaultValue: false
        },
        picture: {
            type: DataTypes.TEXT('long'),
            validate: {
                isString (val) {
                    if(typeof val !== 'string') {
                        throw new sequelize.ValidationError('avatar must be a string')
                    }
                }
            }
        }
    })
    Team.associate = function (models) {
        Team.hasMany(models.teamWall)
        Team.hasMany(models.TeamMembers)
    }
    Team.prototype.updateUsername = function(username) {
        if(typeof username !== 'string') {
            throw new sequelize.ValidationError('Please enter your new username')
        }
        this.update({username: username, koins: this.koins - 200})
    }
    Team.getWallMeta = function (limit) {
            let Post = sequelize.models.teamWall
            let meta = {}

            let nextId = pagination.getNextIdDesc(Post, { userId: this.id }, this.Posts)

            if(nextId === null) {
                meta.nextURL = null
                meta.nextPostsCount = 0
            } else {
                meta.nextURL =
                    ``

                meta.nextPostsCount = pagination.getNextCount(
                    Post, this.Post, limit,
                    { Post: this.id },
                    true
                )
            }

            return meta
    }
    return Team
}
