let shipping = require('./shipping')
let fs = require('fs')

let rules = [
    {
        state: 'interstate',
        town: 'capital_city',
        standard_price: '0',
        express_price: '500'
    }, {
        state: 'interstate',
        town: 'major_town',
        standard_price: '500',
        express_price: '1000'
    }, {
        state: 'interstate',
        town: 'remote',
        standard_price: '800',
        express_price: '1300'
    }, {
        state: 'same_state',
        town: 'local',
        standard_price: '0',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'major_town',
        standard_price: '0',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'remote',
        standard_price: '800',
        express_price: '1300'
    }
]

// eParcel rules
eParcelPrices = [
    {
        state: 'interstate',
        town: 'capital_city',
        standard_price: '850',
        express_price: '500'
    }, {
        state: 'interstate',
        town: 'major_town',
        standard_price: '1320',
        express_price: '1000'
    }, {
        state: 'interstate',
        town: 'remote',
        standard_price: '1630',
        express_price: '1300'
    }, {
        state: 'same_state',
        town: 'local',
        standard_price: '550',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'major_town',
        standard_price: '840',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'remote',
        standard_price: '1520',
        express_price: '1300'
    }
]

// eParcel rules
eParcelWithSendlePrices = [
    {
        state: 'interstate',
        town: 'capital_city',
        standard_price: '850',
        express_price: '500'
    }, {
        state: 'interstate',
        town: 'major_town',
        standard_price: '945',
        express_price: '1000'
    }, {
        state: 'interstate',
        town: 'remote',
        standard_price: '1630',
        express_price: '1300'
    }, {
        state: 'same_state',
        town: 'local',
        standard_price: '550',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'major_town',
        standard_price: '945',
        express_price: '500'
    }, {
        state: 'same_state',
        town: 'remote',
        standard_price: '1520',
        express_price: '1300'
    }
]


let rate = 0;

let response = {
    locals: {
        rules: rules
    },
    setHeader: (_, __) => { },
    status: (_) => {
        return {
            send: (rates) => {
                shippingRates = JSON.parse(rates)
                rate = shippingRates.rates[0].total_price
            }
        }
    }
}

let request = {
    body: {
        rate: {
            destination: {
                postal_code: 2264
            },
            items: []
        }
    }
}

// shipping.calculateShipping(request, response)
let rowCount = 0
let calculateWith = (rules) => {
    let promise = new Promise((resolve, reject) => {

        const csvFilePath = '/Users/josh.bondy/Documents/orders_export.csv'
        const csv = require('csvtojson')
        csv()
            .fromFile(csvFilePath)
            .then((jsonObj) => {
                rowCount = jsonObj.length
                let cost = 0
                response.locals.rules = rules
                for (const order of jsonObj) {
                    if (parseInt(order['Shipping Zip'].replace("'","")) > 0) {
                        request.body.rate.destination.postal_code = order['Shipping Zip']
                        shipping.calculateShipping(request, response)
                        cost = cost + parseInt(rate)
                    }
                }
                return cost
            }).then((total) => {
                resolve(total)
            })

    })
    return promise
}

let formatMoney = input => {
    return `$${(input / 100).toFixed(2)}`
}

calculateWith(eParcelPrices).then(eParcelTotal => {
    console.log(`During the month of may we spent ${formatMoney(eParcelTotal)} on postage`)
    calculateWith(rules).then(result => {
        console.log(`With proposed rules, we would have recovered ${formatMoney(result)}`)
        console.log(`This would bring the average parcel cost from ${formatMoney(eParcelTotal / rowCount)} to ${formatMoney((eParcelTotal - result) / rowCount)}`)
        calculateWith(eParcelWithSendlePrices).then(sendleresult => {
            console.log(`Using Sendle this would bring come to ${formatMoney((sendleresult - result) / rowCount)}`)

            console.log(eParcelTotal);
            console.log(sendleresult);
        })
    })
    
})