const moment = require("moment")
require('moment/locale/id');
const db = require("../models")

moment.locale('id');

const getContent = async (type, datawhmcs) => {
    const gettemplate = await db.reference.findOne({
        where: {
            title : type
        }
    })
    if(!gettemplate) throw new Error('template not found')
    
    const template = gettemplate.value

    let item = ''
    const items = datawhmcs.items
    items.map((data, index) => {
        item += `${data.description} ${data.amount}\n`
    });

    let content = template.replace(/\$name/g, datawhmcs.client_name)
                    .replace(/\$inv_date/g, datawhmcs.inv_date)
                    .replace(/\$inv_id/g, datawhmcs.inv_id)
                    .replace(/\$inv_item/g, item)
                    .replace(/\$inv_amount/g, datawhmcs.total)
                    .replace(/\$inv_duedate/g, datawhmcs.inv_duedate)
    return content
}


module.exports = {
    getContent
}