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

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

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

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

    const webSiteRepo = await chevre.repository.WebSite.createInstance(mongoose.connection);

    if (typeof WEBSITE_IDENTIFIER !== 'string') {
        throw new Error('WEBSITE_IDENTIFIER undefined');
    }

    const creatingWebSites: Pick<
        chevre.factory.creativeWork.certification.webSite.ICertification,
        'about' | 'certificationStatus' | 'project' | 'auditDate'
    >[] = [
            {
                project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
                about: {
                    identifier: WEBSITE_IDENTIFIER,
                    typeOf: chevre.factory.creativeWorkType.WebSite
                },
                certificationStatus: chevre.factory.CertificationStatusEnumeration.CertificationInactive
            }
        ];
    for (const creatingWebSite of creatingWebSites) {
        console.log('updating...', creatingWebSite);
        try {
            const { id } = await webSiteRepo.save({
                attributes: creatingWebSite
            });
            await webSiteRepo.save({
                id,
                attributes: {
                    ...creatingWebSite,
                    auditDate: new Date(),
                    certificationStatus: chevre.factory.CertificationStatusEnumeration.CertificationActive
                }
            });
            console.log('updated.');
        } catch (error) {
            console.error(error);
        }
    }
}

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