require('dotenv').config();
const { Telegraf } = require('telegraf');
const { createClient } = require('@supabase/supabase-js');
const s = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
const msg = '🎯 New Tasks Available!\n\n⭐ Star Loopuman on 8004scan - earn 2 cents\n✅ Validate our agent - earn 5 cents\n\nOpen @LoopumanBot then tap Available Tasks to claim now!';
s.from('app_users').select('telegram_id').not('telegram_id','is',null).then(({data,error}) => {
  if(error) return console.log('Error:', error.message);
  console.log('Sending to', data.length, 'workers...');
  data.forEach((u, i) => {
    setTimeout(() => {
      bot.telegram.sendMessage(u.telegram_id, msg).catch(e => console.log('Failed:', u.telegram_id, e.message));
    }, i * 300);
  });
  setTimeout(() => console.log('Broadcast complete!'), data.length * 300 + 2000);
});
