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

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

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

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

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

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

  return { scheduled: candidates.length };
}
