require('dotenv').config();
const { createClient } = require('@supabase/supabase-js');

const supabase = createClient(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_SERVICE_KEY
);

async function backdateTasks() {
  const { data: tasks } = await supabase
    .from('tasks')
    .select('id, title')
    .in('category', ['research', 'writing', 'translation'])
    .eq('current_workers', 0)
    .eq('status', 'active')
    .limit(5);
  
  if (!tasks || tasks.length === 0) {
    console.log('No eligible tasks found');
    return;
  }
  
  console.log('Found', tasks.length, 'tasks to backdate\n');
  
  const fourDaysAgo = new Date(Date.now() - 4 * 24 * 60 * 60 * 1000).toISOString();
  
  for (const task of tasks) {
    const { error } = await supabase
      .from('tasks')
      .update({ created_at: fourDaysAgo })
      .eq('id', task.id);
    
    if (error) {
      console.log('❌ Failed:', task.title, error.message);
    } else {
      console.log('✅ Backdated:', task.title);
    }
  }
  
  console.log('\n🤖 Now restart AI agents: pm2 restart ai-agents');
}

backdateTasks();
