const moduleGenerator = require('./template/prompt')
const mysql = require('mysql')

module.exports = (plop) => {
  plop.setHelper('upperCase', (txt) => txt.toUpperCase())

  plop.setHelper('toJson', function (obj) {
    return JSON.stringify(obj)
  })

  const connection = mysql.createConnection({
    host: '192.168.0.252',
    port: 3306,
    user: 'root',
    password: 'Dashoo#190801@ali',
    database: 'dashoo_crm',
  })
  // or do async things inside of an action
  // eslint-disable-next-line no-unused-vars
  plop.setActionType('queryTableColumns', function (answers, config, plop) {
    return new Promise((resolve, reject) => {
      const sql =
        'select column_name prop,column_comment label,data_type type,"auto" width,1 sortable from information_schema.columns where table_name=? and table_schema="dashoo_crm"'
      connection.query(sql, answers['table'], function (err, result) {
        if (err) {
          return reject(err)
        }
        answers['columns'] = result
        resolve(result)
      })
      connection.end()
    })
  })
  plop.setGenerator('module', moduleGenerator)
}
