let createDOMPurify = require('dompurify');
let { JSDOM } = require('jsdom');

let window = new JSDOM('').window;
let DOMPurify = createDOMPurify(window);
var escaped_str = require('querystring')
const Errors = require('../lib/errors')
let pagination = require('../lib/pagination.js')
let crypto = require("crypto");

module.exports = (sequelize, DataTypes) => {
    let TeamInvite = sequelize.define('TeamInvite', {
        TeamId: {
            type: DataTypes.BIGINT,
            allowNull: false
        },
        code: {
            type: DataTypes.TEXT,
            unique: true
        },
        maxUses: {
            type: DataTypes.BIGINT,
            default: 0,
            defaultValue: 0,
            validate: {
                len: {
                    args: [0, 1000],
                    msg: "The maximum amount of invite uses are from 0 (unlimited) to 1000!"
                }
            }
        },
        uses: {
            type: DataTypes.BIGINT,
            default: 0,
            defaultValue: 0
        },
        RoleId: {
            type: DataTypes.BIGINT,
            allowNull: true,
            defaultValue: 0,
            default: 0
        },
        UserId: {
            type: DataTypes.BIGINT
        }
    })
    TeamInvite.associate = function (models) {
        TeamInvite.belongsTo(models.Team)
        TeamInvite.belongsTo(models.User)
    }
    TeamInvite.includeOptions = function () {
        let models = sequelize.models

        return [
            {model: models.User, as: 'User', attributes: ['username', 'createdAt', 'id', 'color', 'picture']},
            {model: models.TeamRoles},
        ]
    }
    TeamInvite.prototype.killInvite = function (invite) {
        this.destroy()
    }
    TeamInvite.prototype.createInvite = function (team) {
        this.create({TeamId: team.id, RoleId: team.RoleId, maxUses: team.maxUses})
    }
    return TeamInvite
}
