import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
  const ROUNDS = 10

  const users = [
    {
      name: 'Usuário Padrão',
      email: 'user@user.com',
      password: '12345678',
      role: 'USER' as const,
    },
    {
      name: 'Admin',
      email: 'ruibto@gmail.com',
      password: 'Rc362514',
      role: 'ADMIN' as const,
    },
  ]

  for (const u of users) {
    const passwordHash = await bcrypt.hash(u.password, ROUNDS)
    const existing = await prisma.user.findUnique({ where: { email: u.email } })

    if (existing) {
      await prisma.user.update({
        where: { email: u.email },
        data: { passwordHash, name: u.name, role: u.role },
      })
      console.log(`✅ Atualizado: ${u.email}`)
    } else {
      await prisma.user.create({
        data: { name: u.name, email: u.email, passwordHash, role: u.role },
      })
      console.log(`✅ Criado: ${u.email} (${u.role})`)
    }
  }
}

main()
  .catch((e) => { console.error(e); process.exit(1) })
  .finally(() => prisma.$disconnect())
