import { prisma } from '../lib/db';

async function checkStaffUser() {
  try {
    console.log('Checking for staff user "asim"...\n');

    const user = await prisma.users.findUnique({
      where: { username: 'asim' },
      select: {
        id: true,
        username: true,
        email: true,
        name: true,
        role: true,
        isActive: true,
        failedAttempts: true,
        lockedUntil: true,
        lastLoginAt: true,
        password: true,
      },
    });

    if (!user) {
      console.log('❌ User "asim" not found in database');
      console.log('\nSearching for any staff/admin users...\n');

      const staffUsers = await prisma.users.findMany({
        where: {
          OR: [
            { role: 'STAFF' },
            { role: 'ADMIN' },
          ],
        },
        select: {
          id: true,
          username: true,
          email: true,
          name: true,
          role: true,
          isActive: true,
        },
      });

      if (staffUsers.length === 0) {
        console.log('❌ No staff or admin users found in database');
        console.log('\nTo create a staff user, run:');
        console.log('npm run create-staff-user');
      } else {
        console.log(`Found ${staffUsers.length} staff/admin users:`);
        staffUsers.forEach((u) => {
          console.log(`\n  - Username: ${u.username || 'N/A'}`);
          console.log(`    Email: ${u.email}`);
          console.log(`    Role: ${u.role}`);
          console.log(`    Active: ${u.isActive ? '✅' : '❌'}`);
        });
      }
    } else {
      console.log('✅ User found:');
      console.log(`\n  ID: ${user.id}`);
      console.log(`  Username: ${user.username}`);
      console.log(`  Email: ${user.email}`);
      console.log(`  Name: ${user.name || 'N/A'}`);
      console.log(`  Role: ${user.role}`);
      console.log(`  Active: ${user.isActive ? '✅ Yes' : '❌ No'}`);
      console.log(`  Failed Attempts: ${user.failedAttempts}`);
      console.log(`  Locked Until: ${user.lockedUntil || 'Not locked'}`);
      console.log(`  Last Login: ${user.lastLoginAt || 'Never'}`);
      console.log(`  Has Password: ${user.password ? '✅ Yes' : '❌ No'}`);

      if (!user.isActive) {
        console.log('\n⚠️  WARNING: User account is INACTIVE');
      }

      if (user.lockedUntil && new Date() < user.lockedUntil) {
        console.log('\n⚠️  WARNING: User account is LOCKED');
      }

      if (!user.password) {
        console.log('\n⚠️  WARNING: User has no password set');
      }
    }

  } catch (error) {
    console.error('❌ Error checking user:', error);
  } finally {
    await prisma.$disconnect();
  }
}

checkStaffUser();
