// tslint:disable:no-console
import * as mongoose from 'mongoose';

import { chevre } from '../../../lib/index';

const project = { id: String(process.env.PROJECT_ID) };

async function main() {
    await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });

    const actionRepo = await chevre.repository.Action.createInstance(mongoose.connection);
    const messageRepo = await chevre.repository.Message.createInstance(mongoose.connection);
    const projectRepo = await chevre.repository.Project.createInstance(mongoose.connection);
    const sendGridCredentials = await chevre.credentials.SendGrid.createInstance({
        apiKey: <string>process.env.SENDGRID_API_KEY
    });

    const result = await (await chevre.service.notification.createService()).sendEmailMessage({
        agent: { id: project.id, typeOf: chevre.factory.organizationType.Project },
        object: {
            text: 'sample',
            toRecipient: [{
                name: 'sample',
                email: 'xxx'
            }],
            typeOf: chevre.factory.creativeWorkType.EmailMessage,
            sender: {
                name: 'xxx',
                email: 'xxx'
            },
            about: {
                identifier: <any>'sample',
                name: 'sample',
                typeOf: 'Thing'
            }
        },
        project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
        purpose: <any>{},
        recipient: { id: 'xxx', typeOf: chevre.factory.creativeWorkType.WebApplication },
        typeOf: chevre.factory.actionType.SendAction
    })(
        {
            action: actionRepo,
            message: messageRepo,
            project: projectRepo
        },
        {
            sendGrid: sendGridCredentials
        }
    );
    // tslint:disable-next-line:no-null-keyword
    console.dir(result, { depth: null });
}

main()
    .then(console.log)
    .catch((error) => {
        // tslint:disable-next-line:no-null-keyword
        console.dir(error, { depth: null });
    });
