// tslint:disable:no-implicit-dependencies no-console
import { chevre } from '../../../lib/index';

import * as  moment from 'moment';
import * as  mongoose from 'mongoose';

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

mongoose.Model.on('index', (...args) => {
    console.error('******** index event emitted. ********\n', args);
});

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

    const now = new Date();
    const limit = 100;
    const page = 1;
    const aggregationRepo = await chevre.repository.Aggregation.createInstance(mongoose.connection);
    const aggregations = await aggregationRepo.search({
        limit,
        page,
        typeOf: { $eq: 'AggregateAuthorizeEventServiceOfferAction' },
        aggregateDuration: { $eq: 'P1D' },
        aggregateStart: {
            $gte: moment(now)
                .add(-1, 'months')
                .toDate(),
            $lte: now
        }
    });

    // console.log('aggregations:', aggregations);
    console.log(aggregations.length, 'aggregations found');

    // const cleanResult = await aggregationRepo.deleteAggregateStartPassedCertainPeriod({
    //     aggregateStart: {
    //         $lt: moment()
    //             .add(-1, 'year')
    //             .toDate()
    //     }
    // });
    // console.log('cleanResult:', cleanResult);
}

main()
    .then(() => {
        console.log('success!');
    })
    .catch(console.error);
