const { createClient } = require('@supabase/supabase-js');
require('dotenv').config();
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);

(async () => {
  // === STEP 1: Cancel broken/test tasks ===
  const cancelTitles = [
    'Classify this image',
    'Test: Describe blue',
    '🧪 TEST FOR RAY: Accept this task!',
    '🧪 FLOW TEST: Reply YES if you see this',
    'Test: Is this API working?',
  ];
  
  for (const title of cancelTitles) {
    const { data, error } = await supabase.from('tasks')
      .update({ status: 'cancelled' })
      .eq('title', title)
      .in('status', ['open', 'active', 'full'])
      .select('id, title');
    if (data?.length) console.log('❌ Cancelled:', title);
  }

  // Cancel stuck local tasks
  const { data: localCancelled } = await supabase.from('tasks')
    .update({ status: 'cancelled' })
    .in('title', [
      'Collect business cards from 10 local restaurants',
      'Check product availability at 3 supermarkets',
      'Take 10 photos of menu boards at local coffee shops',
    ])
    .eq('status', 'active')
    .select('id, title');
  localCancelled?.forEach(t => console.log('❌ Cancelled stuck local:', t.title));

  console.log('\n✅ Cleanup done\n');

  // === STEP 2: Show remaining open tasks ===
  const { data: remaining } = await supabase.from('tasks')
    .select('id, title, status, category')
    .in('status', ['open', 'active'])
    .order('created_at', { ascending: false });
  
  console.log('=== REMAINING OPEN/ACTIVE TASKS ===');
  remaining?.forEach(t => console.log(t.status.padEnd(8), '|', (t.category||'').padEnd(12), '|', t.title?.substring(0, 60)));
  console.log('Total:', remaining?.length);
})();
