// EmailService.js
var sendgrid = require("sendgrid");

exports.sendOrderEmail = function(inviteeData) {
    Run.findOne({ id: inviteeData.run_id }, function(err, runData) {
        if (err) {
            console.log(err);
            return;
        }

        var emailText =  "You've been invited to " + runData.creatorName + "'s lunch run: '"
                    + runData.name + "'! \n\n Put in your order here:\nhttp://www.lunchrun.me/#/order/"
                    + inviteeData.id;
                    
        var options = {
            fromname: runData.creatorName + " via LunchRun.me",
            from: sails.config.email.fromAddress,
            to: inviteeData.email,
            toname: inviteeData.name,
            subject: "You've been invited to a lunch run!",
            replyto: runData.creatorEmail,
            text: emailText
        };
        var sg = sendgrid(sails.config.email.apiUser, sails.config.email.apiKey);

        sg.send(options, function(err, response) {
            if (err) {
                console.log(err);
            } else {
                console.log("message sent from: " + runData.creatorEmail + ", to: " + inviteeData.email);
            }
        });

        console.log(options);
    });
};
