//
// Copyright 2021 Vulcanize, Inc.
//

import { ResetWatcherCmd } from '@cerc-io/cli';
{{#if (subgraphPath)}}
import { getGraphDbAndWatcher } from '@cerc-io/graph-node';
{{/if}}

import { Database{{#if (subgraphPath)}}, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP{{/if}} } from '../../database';
import { Indexer } from '../../indexer';

export const command = 'watcher';

export const desc = 'Reset watcher to a block number';

export const builder = {
  blockNumber: {
    type: 'number'
  }
};

export const handler = async (argv: any): Promise<void> => {
  const resetWatcherCmd = new ResetWatcherCmd();
  await resetWatcherCmd.init(argv, Database);

  {{#if (subgraphPath)}}
  const { graphWatcher } = await getGraphDbAndWatcher(
    resetWatcherCmd.config.server,
    resetWatcherCmd.clients.ethClient,
    resetWatcherCmd.ethProvider,
    resetWatcherCmd.database.baseDatabase,
    ENTITY_QUERY_TYPE_MAP,
    ENTITY_TO_LATEST_ENTITY_MAP
  );

  {{/if}}
  await resetWatcherCmd.initIndexer(Indexer{{#if (subgraphPath)}}, graphWatcher{{/if}});

  await resetWatcherCmd.exec();
};
