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

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

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

// tslint:disable-next-line:max-func-body-length
async function main() {
    await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });

    const actionRepo = await chevre.repository.Action.createInstance(mongoose.connection);
    const action = await actionRepo.start({
        project: { typeOf: chevre.factory.organizationType.Project, id: project.id },
        typeOf: chevre.factory.actionType.CreateAction,
        agent: {
            id: project.id,
            name: 'sample',
            typeOf: chevre.factory.organizationType.Project
        },
        object: { typeOf: chevre.factory.offerType.Offer },
        ...{
            targetCollection: { typeOf: chevre.factory.offerType.Offer }
        }
    });
    console.log('actions started,', action);
    // const error = new Error('sample error');
    // await actionRepo.giveUp({
    //     typeOf: action.typeOf,
    //     id: action.id,
    //     // error: { ...error, message: error.message, name: error.name }
    //     error
    // });
    const result = await actionRepo.completeWithVoid({
        typeOf: action.typeOf,
        id: action.id,
        result: {}
    });
    console.log('actions completed', result);
}

main()
    .then()
    .catch(console.error);
