import { PrismaClient } from '../prisma/client-generated'

const prisma = new PrismaClient()

async function main() {
    console.log('Updating user settings to show recent notes...')

    const updateResult = await prisma.userAISettings.updateMany({
        data: {
            showRecentNotes: true
        }
    })

    console.log(`Updated ${updateResult.count} user settings.`)

    // Verify and Create missing
    const users = await prisma.user.findMany({
        include: { aiSettings: true }
    })

    for (const u of users) {
        if (!u.aiSettings) {
            console.log(`User ${u.id} has no settings. Creating default...`)
            await prisma.userAISettings.create({
                data: {
                    userId: u.id,
                    showRecentNotes: true
                }
            })
            console.log(`Created settings for ${u.id}`)
        } else {
            console.log(`User ${u.id}: showRecentNotes = ${u.aiSettings.showRecentNotes}`)
        }
    }
}

main()
    .catch(e => console.error(e))
    .finally(async () => await prisma.$disconnect())
