const { PrismaClient } = require('@prisma/client');

async function fixSystemUser() {
  const prisma = new PrismaClient({
    datasources: {
      db: {
        url: 'file:./prisma/dev.db'
      }
    }
  });
  
  try {
    // Check if system user exists
    let systemUser = await prisma.users.findUnique({
      where: { id: 'system' }
    });
    
    if (!systemUser) {
      console.log('🔧 Creating system user for Enhanced Risk Manager...');
      systemUser = await prisma.users.create({
        data: {
          id: 'system',
          email: 'system@enhanced-risk-manager.ai',
          name: 'Enhanced Risk Manager System',
          createdAt: new Date(),
          updatedAt: new Date()
        }
      });
      console.log('✅ System user created successfully!');
    } else {
      console.log('✅ System user already exists');
    }
    
    // Check current users
    const users = await prisma.users.findMany();
    console.log(`📊 Total users in database: ${users.length}`);
    users.forEach(user => {
      console.log(`   - ${user.id}: ${user.email}`);
    });
    
  } catch (error) {
    console.error('❌ Error:', error.message);
  } finally {
    await prisma.$disconnect();
  }
}

fixSystemUser();
