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

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

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

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

// tslint:disable-next-line:max-func-body-length
async function main() {
    const aggregateOfferRepo = await chevre.repository.AggregateOffer.createInstance(mongoose.connection);

    const cursor = aggregateOfferRepo.getCursor(
        {
            'project.id': { $ne: excludedProject.id }
            // _id: { $eq: 'blyk9q24f' }
        },
        {
            _id: 1,
            project: 1
        }
    );
    console.log('docs found');

    let i = 0;
    await cursor.eachAsync(async (doc) => {
        i += 1;
        const offer: Pick<chevre.factory.aggregateOffer.IAggregateOffer, 'id' | 'project'> = doc.toObject();

        console.log('count?', offer.project.id, offer.id, i);
        const result = await aggregateOfferRepo.countIncludedInDataCatalog({
            id: { $eq: String(offer.id) }
        });
        console.log('result:', result, offer.project.id, offer.id, i);
    });

    console.log(i, 'docs checked');

    const resultMax = await aggregateOfferRepo.maxIncludedInDataCatalogCount();
    console.log('resultMax:', resultMax);
}

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