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

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 Seeding database...');

  const hashedPassword = await bcrypt.hash('password', 12);

  // Super Admin (not tied to any restaurant)
  await prisma.user.upsert({
    where: { email: 'superadmin@restobill.com' },
    update: {},
    create: {
      name: 'Super Admin',
      email: 'superadmin@restobill.com',
      password: hashedPassword,
      role: 'SUPER_ADMIN',
    },
  });

  // Test Restaurant
  let restaurant = await prisma.restaurant.findFirst({
    where: { name: 'Test Restaurant' },
  });

  if (!restaurant) {
    restaurant = await prisma.restaurant.create({
      data: {
        name: 'Test Restaurant',
        address: '1, Test Street, Test City',
        phone: '9876543210',
      },
    });
  }

  // Admin for Test Restaurant
  await prisma.user.upsert({
    where: { email: 'admin@testrestaurant.com' },
    update: {},
    create: {
      name: 'Admin User',
      email: 'admin@testrestaurant.com',
      password: hashedPassword,
      role: 'ADMIN',
      restaurantId: restaurant.id,
    },
  });

  // Staff for Test Restaurant
  await prisma.user.upsert({
    where: { email: 'staff@testrestaurant.com' },
    update: {},
    create: {
      name: 'Staff User',
      email: 'staff@testrestaurant.com',
      password: hashedPassword,
      role: 'STAFF',
      restaurantId: restaurant.id,
    },
  });

  console.log('✅ Seeding complete!');
  console.log('');
  console.log('📋 Login Credentials:');
  console.log('   Super Admin : superadmin@restobill.com  / password');
  console.log('   Admin       : admin@testrestaurant.com  / password');
  console.log('   Staff       : staff@testrestaurant.com  / password');
  console.log('');
  console.log('💡 Use the Import Excel feature in Menu Management to add your menu items.');
}

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