
import prisma from './src/lib/prisma';

(async () => {
    try {
        const email = process.argv[2];
        if (!email) {
            console.error('Please provide email');
            process.exit(1);
        }

        let user;
        for (let i = 0; i < 10; i++) {
            user = await prisma.user.findUnique({ where: { email } });
            if (user) break;
            console.log(`User ${email} not found, retrying (${i + 1}/5)...`);
            await new Promise(resolve => setTimeout(resolve, 1000));
        }

        if (!user) {
            console.error(`User ${email} not found after retries. CWD: ${process.cwd()} DB: ${process.env.DATABASE_URL}`);
            process.exit(1);
        }

        await prisma.user.update({
            where: { email },
            data: {
                role: 'ADMIN'
            }
        });
        console.log(`User ${email} promoted to ADMIN`);
    } catch (e) {
        console.error('Error:', e);
        process.exit(1);
    } finally {
        await prisma.$disconnect();
    }
})();
