const voucher_codes = require('voucher-code-generator');
const {
    UserModel,
    PromocodeModel
} = global.config
const {
    generateVoucher
} = global.utils

module.exports = async (ctx) => {
    let user = await UserModel.findByPk(ctx.from.id)
    if(user.status != 'admin') return await ctx.reply(`Admin Only.`)
    ctx.args = ctx.update.message.text.split(' ')
    if (!ctx.args[1] || !ctx.args[2]) return ctx.reply(`/genpromo [activations] [money]`)
    let code = voucher_codes.generate({
        length: 6,
        count: 1,
        prefix: "CMP-",
        postfix: "-FIRE",
        charset: voucher_codes.charset("alphanumeric")
    });
    let promocode = await PromocodeModel.findOne({
        where: {
          code: code[0]
        }
      })
    if(promocode === null){
    await PromocodeModel.create({
        code: code[0],
        activations: ctx.args[1],
        money: ctx.args[2]
    })
    let voucherImage = await generateVoucher(code, ctx.args[1], ctx.args[2])
    return await ctx.sendPhoto({source: voucherImage, caption: `Создан промокод ${code[0]}` })
}
}