require('dotenv').config();
const { createClient } = require('@supabase/supabase-js');
const sb = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);
(async () => {
  // Find ALL non-active users
  const { data: all } = await sb.from('app_users')
    .select('id, first_name, telegram_id, status, tasks_abandoned');
  
  const nonActive = all.filter(u => u.status !== 'active' && u.status !== null);
  console.log('Non-active users:', nonActive.length);
  for (const u of nonActive) {
    console.log('  ', u.first_name || 'N/A', '| TG:', u.telegram_id, '| Status:', u.status, '| Abandoned:', u.tasks_abandoned);
  }
  
  // Unsuspend all
  for (const u of nonActive) {
    await sb.from('app_users').update({ 
      status: 'active',
      tasks_abandoned: 0,
      updated_at: new Date().toISOString()
    }).eq('id', u.id);
    console.log('  ✅ Unsuspended:', u.first_name || u.telegram_id);
  }
  
  if (nonActive.length === 0) {
    console.log('All users are already active. Checking if suspension is in-app state...');
    // Maybe the bot uses an in-memory or session-based suspension
    const ray = all.find(u => u.telegram_id === '8367990869');
    if (ray) {
      console.log('Your account:', ray.first_name, '| Status:', ray.status, '| Abandoned:', ray.tasks_abandoned);
      // Reset abandoned count anyway
      await sb.from('app_users').update({ tasks_abandoned: 0 }).eq('id', ray.id);
      console.log('✅ Reset abandoned count to 0');
    }
  }
})();
