// 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 productRepo = await chevre.repository.Product.createInstance(mongoose.connection);

    const result = await productRepo.upsertManyByProductId(
        [
            {
                $set: {
                    project: {
                        typeOf: chevre.factory.organizationType.Project,
                        id: PROJECT_ID
                    },
                    typeOf: chevre.factory.product.ProductType.EventService,
                    productID: '2023122601EventService',
                    name: {
                        en: 'xxx',
                        ja: 'xxx'
                    },
                    // hasOfferCatalog: {
                    //     id: 'blpc770py',
                    //     typeOf: 'OfferCatalog'
                    // },
                    // serviceType: {
                    //     codeValue: '0001',
                    //     typeOf: 'CategoryCode',
                    //     inCodeSet: {
                    //         identifier: chevre.factory.categoryCode.CategorySetIdentifier.ServiceType,
                    //         typeOf: 'CategoryCodeSet'
                    //     }
                    // },
                    availableChannel: {
                        typeOf: 'ServiceChannel',
                        credentials: {}
                    },
                    description: {
                        en: 'xxx',
                        ja: 'xxx'
                    }
                },
                $unset: {
                    hasOfferCatalog: 1,
                    serviceType: 1
                }
            },
            {
                $set: {
                    project: {
                        typeOf: chevre.factory.organizationType.Project,
                        id: PROJECT_ID
                    },
                    typeOf: chevre.factory.product.ProductType.EventService,
                    productID: '2023122602EventService',
                    name: {
                        en: 'xxx',
                        ja: 'xxx'
                    },
                    hasOfferCatalog: {
                        // id: 'blpc770py',
                        typeOf: 'OfferCatalog',
                        itemListElement: [{ id: 'blpc770py' }]
                    },
                    // serviceType: {
                    //     codeValue: '0001',
                    //     typeOf: 'CategoryCode',
                    //     inCodeSet: {
                    //         identifier: chevre.factory.categoryCode.CategorySetIdentifier.ServiceType,
                    //         typeOf: 'CategoryCodeSet'
                    //     }
                    // },
                    availableChannel: {
                        typeOf: 'ServiceChannel',
                        credentials: {}
                    },
                    description: {
                        en: 'xxx',
                        ja: 'xxx'
                    }
                },
                $unset: {
                }
            }
        ]
        // { replace: true }
    );
    // tslint:disable-next-line:no-null-keyword
    console.log(result);
}

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