import axios from 'axios';

const BOT_TOKEN = '8410461852:AAGXQCwRPFbndqwTgLJh8kYxST4Z0vgh72U';
// const CHAT_IDS = ['5147700658', '-5075171894'];
const CHAT_IDS = [ '-5075171894'];
const TELEGRAM_API_URL = `https://api.telegram.org/bot${BOT_TOKEN}`;

/**
 * Send a message to Telegram bot
 * @param message The message text to send
 * @returns Promise<boolean> indicating success or failure
 */
export const sendTelegramMessage = async (message: string): Promise<boolean> => {
  try {
    const results = await Promise.all(
      CHAT_IDS.map(async (chatId) => {
        try {
          const response = await axios.post(`${TELEGRAM_API_URL}/sendMessage`, {
            chat_id: chatId,
            text: message,
            parse_mode: 'HTML',
          });

          if (response.data && response.data.ok) {
            console.log(`Telegram message sent successfully to ${chatId}`);
            return true;
          } else {
            console.error(`Telegram API error for ${chatId}:`, response.data);
            return false;
          }
        } catch (error) {
          console.error(`Failed to send Telegram message to ${chatId}:`, error);
          return false;
        }
      })
    );

    // Return true if at least one message was sent successfully
    return results.some((result) => result);
  } catch (error) {
    console.error('Failed to send Telegram message:', error);
    return false;
  }
};
