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

(async () => {
  try {
    console.log('\n📊 Fetching remaining tasks...\n');
    
    const { data: tasks, error } = await sb
      .from('tasks')
      .select('category, title, max_workers, source')
      .order('category');

    if (error) {
      console.error('❌ Database error:', error.message);
      return;
    }

    if (!tasks || tasks.length === 0) {
      console.log('⚠️  No tasks found in database');
      return;
    }

    const byCategory = {};
    tasks.forEach(t => {
      if (!byCategory[t.category]) {
        byCategory[t.category] = [];
      }
      byCategory[t.category].push(t);
    });

    console.log('📊 REMAINING TASKS BY CATEGORY:\n');
    
    let totalTasks = 0;
    Object.keys(byCategory).sort().forEach(cat => {
      const count = byCategory[cat].length;
      totalTasks += count;
      
      console.log(`${cat.toUpperCase()} (${count} tasks):`);
      byCategory[cat].slice(0, 2).forEach((t, i) => {
        console.log(`  ${i+1}. ${t.title.substring(0, 65)}`);
      });
      if (byCategory[cat].length > 2) {
        console.log(`  ... and ${byCategory[cat].length - 2} more\n`);
      } else {
        console.log('');
      }
    });

    console.log(`${'='.repeat(70)}`);
    console.log(`✅ TOTAL REMAINING: ${totalTasks} tasks`);
    console.log(`${'='.repeat(70)}\n`);

  } catch (err) {
    console.error('❌ Error:', err.message);
  }
})();
