const {
  Telegraf
} = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
const {
  PromocodeModel,
  mainChat
} = global.config
const voucher_codes = require('voucher-code-generator');
const rand = require('./rand')
const generateVoucher = require('./generateVoucher');
const { promoTopicId } = require('/config');
module.exports = async () => {
        let activs = rand(1, 3)
        let give = rand(1000, 10000)
        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: activs,
            money: give
        })
        let voucherImage = await generateVoucher(code[0], activs, give)
        return await bot.telegram.sendPhoto(mainChat, {source: voucherImage}, {reply_to_message_id: promoTopicId})
    }
}