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

const prisma = new PrismaClient()

async function main() {
  console.log('🧹 Cleaning database...')

  // Delete ALL data from all tables (order matters due to foreign keys)
  await prisma.notification.deleteMany()
  await prisma.supervisorResponse.deleteMany()
  await prisma.reviewPhoto.deleteMany()
  await prisma.review.deleteMany()
  await prisma.user.deleteMany()

  console.log('  ✅ All data deleted')

  // ════════════════════════════════════════════════════════
  // ── Create Admin ──
  // ════════════════════════════════════════════════════════
  const adminPasswordHash = await bcrypt.hash('%%S0p0rt326', 12)

  const admin = await prisma.user.create({
    data: {
      email: 'admin@totes.com.bo',
      name: 'Administrador Totes',
      phone: '+591 2 2000000',
      passwordHash: adminPasswordHash,
      role: UserRole.ADMIN,
      isActive: true,
    },
  })
  console.log(`  ✅ Admin: ${admin.name} (${admin.email})`)

  console.log('\n🎉 Clean seed completed successfully!')
}

main()
  .catch((e) => {
    console.error('❌ Clean seed failed:', e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
