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

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

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

chevre.eventEmitter.task.taskEventEmitter.onTaskStatusChanged((event) => {
    console.log('onTaskStatusChanged', event);
});

async function main() {
    const now = new Date();
    await mongoose.connect(<string>process.env.MONGOLAB_URI);

    const taskIdentifier: string = `${project.id}:${chevre.factory.taskName.TriggerWebhook}:sample:2025032801`;
    const taskRepo = await chevre.repository.Task.createInstance(mongoose.connection);
    const creatingTask = {
        alternateName: taskIdentifier,
        identifier: taskIdentifier,
        name: chevre.factory.taskName.TriggerWebhook,
        status: chevre.factory.taskStatus.Ready,
        runsAt: now,
        remainingNumberOfTries: 1,
        numberOfTried: 0,
        executionResults: [],
        data: {
            object: {
                sample: 'sample'
            },
            recipient: {
                id: '',
                name: 'telemetry',
                typeOf: 'WebApplication'
            },
            target: {
                httpMethod: 'POST',
                encodingType: 'application/json',
                typeOf: 'EntryPoint',
                urlTemplate: 'https://smart-theater-telemetry-api.an.r.appspot.com/webhooks/lineNotify'
            },
            identifier: 'sampleNotificationIdentifier'
            // about: {
            //     id: '67e62b0b28817948355a671c',
            //     typeOf: 'PlaceOrder'
            // }
        },
        project: { id: project.id, typeOf: <chevre.factory.organizationType.Project>chevre.factory.organizationType.Project }
    };
    taskRepo.createIfNotExistByAlternateName(creatingTask, { emitImmediately: true });
    taskRepo.createIfNotExistByAlternateName(creatingTask, { emitImmediately: true });
    console.log('success!');
}

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