import type { TeHandler } from '../te.handler';

export async function dataScheduler(this: TeHandler): Promise<{ scheduled: number }> {
  const { logger, mongodb } = this;

  const candidates = await mongodb?.find('teUrls', {
    teType: 'te',
    teSymbol: { $exists: true, $ne: null },
    teChartsDatasource: { $exists: true },
    teChartToken: { $exists: true },
    teLastUpdate: { $exists: true },
    teChart: 'EC',
    $or: [
      { lastDataFetch: { $exists: false } },
      { $expr: { $ne: ['$teLastUpdate', '$lastDataFetchUpdate'] } },
    ],
  }, {
    sort: { lastDataFetch: 1 },
    projection: { teSymbol: 1, url: 1, lastDataFetch: 1 },
  });

  if (!candidates?.length) {
    logger.debug('No symbols need data fetching');
    return { scheduled: 0 };
  }

  logger.info(`Scheduling ${candidates.length} symbols for TE data fetch`);

  for (const doc of candidates) {
    await this.scheduleOperation('te-data-fetch', {
      teSymbol: doc.teSymbol,
      url: doc.url,
      isInitialFetch: !doc.lastDataFetch,
    }, {
      jobId: `data-fetch-${doc.teSymbol}`,
      priority: 8,
    });
  }

  return { scheduled: candidates.length };
}
