import { getRandomUserAgent } from '@stock-bot/utils';
import type { CeoHandler } from '../ceo.handler';

export async function getChannels(this: CeoHandler, payload: number | undefined): Promise<unknown> {
  const proxy = this.proxy?.getProxy();
  if (!proxy) {
    this.logger.warn('No proxy available for CEO channels update');
    return;
  }
  let page;
  if (payload === undefined) {
    page = 1;
  } else {
    page = payload;
  }

  try {
    this.logger.info(`Fetching CEO channels for page ${page} with proxy ${proxy}`);
    const response = await this.http.get(
      'https://api.ceo.ca/api/home?exchange=all&sort_by=symbol&sector=All&tab=companies&page=' +
        page,
      {
        proxy: proxy,
        headers: {
          'User-Agent': getRandomUserAgent(),
        },
      }
    );
    if (!response.ok) {
      this.logger.debug(`Response status: ${response.status}`);
      throw new Error(`Failed to fetch CEO channels: ${response.statusText}`);
    }

    const results = await response.json();
    const channels = results.channel_categories[0].channels;
    const totalChannels = results.channel_categories[0].total_channels;
    const totalPages = Math.ceil(totalChannels / channels.length);
    const exchanges: { exchange: string; countryCode: string }[] = [];
    const symbols = channels.map((channel: any) => {
      // check if exchange is in the exchanges array object
      if (!exchanges.find((e: any) => e.exchange === channel.exchange)) {
        exchanges.push({
          exchange: channel.exchange,
          countryCode: 'CA',
        });
      }
      const details = channel.company_details || {};
      return {
        symbol: channel.symbol,
        exchange: channel.exchange,
        name: channel.title,
        type: channel.type,
        ceoId: channel.channel,
        marketCap: details.market_cap,
        volumeRatio: details.volume_ratio,
        avgVolume: details.avg_volume,
        stockType: details.stock_type,
        issueType: details.issue_type,
        sharesOutstanding: details.shares_outstanding,
        float: details.float,
        lastSeen: new Date(),
        active: true,
      };
    });

    await this.mongodb.batchUpsert('ceoSymbols', symbols, ['symbol', 'exchange']);
    await this.mongodb.batchUpsert('ceoExchanges', exchanges, ['exchange']);

    if (page === 1) {
      for (let i = 2; i <= totalPages; i++) {
        this.logger.info(`Scheduling page ${i} of ${totalPages} for CEO channels`);
        await this.scheduleOperation('get-channels', i);
      }
    }

    this.logger.info(`Fetched CEO channels for page ${page}/${totalPages}`);
    return { page, totalPages };
  } catch (error: any) {
    this.logger.error(`Error fetching CEO channels for page ${page} with proxy ${proxy}:`, error);
    throw new Error(`Failed to fetch CEO channels: ${error.message}`);
  }
}
