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

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

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

async function main() {
    await mongoose.connect(<string>process.env.MONGOLAB_URI);

    // const indexes = await mongoose.connection.db.collection('aggregateOffers')
    //     .indexes();
    // console.log(indexes);
    // console.log(indexes.length);

    const offerRepo = await chevre.repository.Offer.createInstance(mongoose.connection);

    const offers = await offerRepo.search(
        {
            limit: 3,
            // page: 1,
            sort: { identifier: chevre.factory.sortType.Descending },
            project: { id: { $eq: String(process.env.PROJECT_ID) } },
            availability: { $eq: chevre.factory.itemAvailability.InStock },
            // acceptedPaymentMethod: { identifier: { $in: ['xxx'] } },
            parentOffer: {
            }
            // priceSpecification: {
            //     appliesToMovieTicket: {
            //         $size: 2,
            //         serviceOutput: {
            //             typeOf: {
            //             }
            //         }
            //     }
            // }
            // includedInDataCatalog: { id: { $in: ['0001'] } }
            // additionalProperty: {
            //     $all: [
            //         {
            //             $elemMatch: {
            //                 name: { $eq: 'theaterCode' },
            //                 value: { $in: ['120'] }
            //             }
            //         },
            //         {
            //             $elemMatch: {
            //                 name: { $eq: 'ticketCode' },
            //                 value: { $in: ['10', '1001'] }
            //             }
            //         }
            //     ]
            // }
        },
        { project: 1, name: 1, identifier: 1 }
    );
    console.log(offers.map((offer) => {
        return `${offer.project?.id} ${offer.id} ${offer.parentOffer?.id} ${offer.identifier} ${offer.name?.ja}`;
    }));
    // console.log(offers);
    console.log(offers.length);
}

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