const sgMail = require('@sendgrid/mail');

const builders = require('./builders');

sgMail.setApiKey(process.env.SG_API_KEY);


const sendEmail = async (sendToAddress, subject, payload) => {
    const respObj = {};
    try {
        sgMail.setApiKey(process.env.SG_API_KEY);
        const msg = {
            to: `${sendToAddress}`,
            from: 'chrispatrickcodes@gmail.com',
            subject: `${subject}`,
            html: payload
        }
        const sgResponse = await sgMail.send(msg);

        if (sgResponse[0].statusCode === 202) {
            respObj.status = 1;
            respObj.msg = 'Email Sent';
            respObj.session = null;
        } else {
            respObj.status = 3;
            respObj.msg = `Error in SG`;
            respObj.session = null;
        }
    }
    catch (err) {
        respObj.status = 3;
        respObj.msg = `${err.message}`;
        respObj.session = null;
    }
    finally {
        return respObj;
    }

}
module.exports = {
    sendRecoveryEmail: async (userObj, tempPass) => {

        const sendToEmail = userObj.email;
        const subject = 'CPCodes Account Recovery Service';
        let payload = '';
        const respObj = {};
        try {
            payload = await builders.recoveryEmailBuilder({tempPass});
            const SEResponse = await sendEmail(sendToEmail, subject, payload);

            if (SEResponse.status === 1 && SEResponse.msg === 'Email Sent') {
                respObj.status = 1;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            } else if (SEResponse.status === 1 && SEResponse.msg !== 'Email Sent') {
                respObj.status = 2;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            } else {
                respObj.status = 3;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            }
        }
        catch (err) {
            console.log(err);
            respObj.status = 500;
            respObj.msg = err.message;
            respObj.session = null;
        } finally {
            return respObj;
        }
    },
    sendContactEmail: async (data) => {

        const sendToEmail = 'chrispatrickcodes@gmail.com';
        const subject = 'New Contact from ChrisPCodes';
        let payload = '';
        const respObj = {};
        try {
            payload = await builders.contactEmailBuilder(data);
            const SEResponse = await sendEmail(sendToEmail, subject, payload);

            if (SEResponse.status === 1 && SEResponse.msg === 'Email Sent') {
                respObj.status = 1;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            } else if (SEResponse.status === 1 && SEResponse.msg !== 'Email Sent') {
                respObj.status = 2;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            } else {
                respObj.status = 3;
                respObj.msg = SEResponse.msg;
                respObj.session = null;
            }
        }
        catch (err) {
            console.log(err);
            respObj.status = 500;
            respObj.msg = err.message;
            respObj.session = null;
        } finally {
            return respObj;
        }
    }
}