#!/usr/bin/env node

import { config } from 'dotenv';
import { sql } from '@vercel/postgres';

// Load environment variables
config({ path: '.env.local' });

async function listUsers() {
  try {
    console.log('📋 Fetching all users from database...\n');
    
    const result = await sql`
      SELECT id, email, role, created_at 
      FROM users 
      ORDER BY created_at DESC
    `;

    if (result.rows.length === 0) {
      console.log('👤 No users found in database');
      return;
    }

    console.log(`Found ${result.rows.length} user(s):\n`);
    
    result.rows.forEach((user, index) => {
      const roleEmoji = user.role === 'admin' ? '👑' : '👤';
      const createdDate = new Date(user.created_at).toLocaleDateString();
      
      console.log(`${index + 1}. ${roleEmoji} ${user.email}`);
      console.log(`   ID: ${user.id}`);
      console.log(`   Role: ${user.role}`);
      console.log(`   Created: ${createdDate}`);
      console.log('');
    });

    const adminCount = result.rows.filter(user => user.role === 'admin').length;
    const userCount = result.rows.filter(user => user.role === 'user').length;

    console.log('📊 Summary:');
    console.log(`   👑 Admins: ${adminCount}`);
    console.log(`   👤 Users: ${userCount}`);
    console.log(`   📈 Total: ${result.rows.length}`);

  } catch (error) {
    console.error('❌ Database error:', error.message);
    process.exit(1);
  }
}

listUsers(); 