const prisma = require('../../src/prisma/PrismaClient.js');

async function seedUserAreas() {
    try {
        // Ambil user dengan role 'sales1' dan 'sales2'
        const sales1 = await prisma.user.findFirst({ where: { username: 'sales1' } });
        const sales2 = await prisma.user.findFirst({ where: { username: 'sales2' } });

        if (!sales1 || !sales2) {
            throw new Error('User sales1 or sales2 not found');
        }

        // Ambil provinsi
        const jawaTengah = await prisma.province.findFirst({ where: { name: 'Jawa Tengah' } });
        const diy = await prisma.province.findFirst({ where: { name: 'DI Yogyakarta' } });

        if (!jawaTengah || !diy) {
            throw new Error('Province Jawa Tengah or DI Yogyakarta not found');
        }

        // Buat user_areas
        await prisma.userArea.createMany({
            data: [
                {
                    user_id: sales1.id,
                    province_id: jawaTengah.id,
                },
                {
                    user_id: sales2.id,
                    province_id: diy.id,
                },
            ],
        });

        console.log('✅ User area seeded');
    } catch (error) {
        console.error('❌ Error seeding user area:', error);
    }
}

module.exports = seedUserAreas;