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

async function checkAdmin() {
    process.env.APP_MODE = 'prod';

    // Attempt to locate database path similar to production
    let dbPath = './server/prod.db';
    if (!fs.existsSync(dbPath)) {
        dbPath = './prod.db'; // If running from inside server dir
    }

    console.log(`Checking database at: ${path.resolve(dbPath)}`);
    console.log(`File exists: ${fs.existsSync(dbPath)}`);

    if (!fs.existsSync(dbPath)) {
        console.error('CRITICAL: Database file not found! Ensure pm2 is running from the correct directory.');
        return;
    }

    const prisma = new PrismaClient({
        datasources: {
            db: {
                url: `file:${path.resolve(dbPath)}`,
            },
        },
    });

    try {
        const admin = await prisma.user.findFirst({
            where: { role: 'ADMIN' },
        });

        if (admin) {
            console.log(`✅ Admin user found: ${admin.email}`);
            console.log(`First login: ${admin.isFirstLogin}`);
        } else {
            console.log('❌ No admin user found in database.');
        }
    } catch (error) {
        console.error('Error connecting to database:', error.message);
    } finally {
        await prisma.$disconnect();
    }
}

checkAdmin();
