import { createClient } from '@supabase/supabase-js';

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

async function checkAITasks() {
  // Check for AI submissions today
  const { data: aiSubmissions, error } = await supabase
    .from('submissions')
    .select('id, task_id, worker_id, status, is_ai, accepted_at, submitted_at')
    .eq('is_ai', true)
    .gte('accepted_at', '2026-01-17T00:00:00Z')
    .order('accepted_at', { ascending: false });

  if (error) {
    console.error('Error:', error);
    return;
  }

  console.log(`📊 AI Submissions Today: ${aiSubmissions?.length || 0}\n`);
  
  if (aiSubmissions && aiSubmissions.length > 0) {
    // Get task details
    const taskIds = [...new Set(aiSubmissions.map(s => s.task_id))];
    const { data: tasks } = await supabase
      .from('tasks')
      .select('id, title, category, budget')
      .in('id', taskIds);
    
    console.log('✅ AI Agent Activity Found!\n');
    aiSubmissions.forEach(s => {
      const task = tasks?.find(t => t.id === s.task_id);
      const time = new Date(s.accepted_at).toLocaleTimeString();
      console.log(`${time} - ${task?.title || 'Unknown'} (${task?.category}) - Status: ${s.status}`);
    });
  } else {
    console.log('❌ No AI submissions found today!');
    console.log('\n📌 This means the "29 tasks accepted" this morning was a FALSE POSITIVE.');
    console.log('📌 The tasks were likely BLOCKED by forbidden_category checks.');
  }
}

checkAITasks().then(() => process.exit(0));
