const axios = require('axios');
const { API_URL, TIMEOUT } = require('./config');

async function callTelegram(method, params) {
  try {
    const { data } = await axios.post(`${API_URL}/${method}`, params, { timeout: TIMEOUT });
    if (!data.ok) throw new Error(`Telegram error: ${data.description}`);
    return data.result;
  } catch (err) {
    console.error('❌ Telegram API error:', err.message);
    return null;
  }
}

async function sendMessage(chatId, text, options = {}) {
  return callTelegram('sendMessage', {
    chat_id: chatId,
    text,
    parse_mode: options.parse_mode || 'HTML',
    disable_web_page_preview: true,
  });
}

async function sendPhoto(chatId, photo, caption = '', options = {}) {
  return callTelegram('sendPhoto', {
    chat_id: chatId,
    photo,
    caption,
    parse_mode: 'HTML',
  });
}

module.exports = { sendMessage, sendPhoto };
