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

const prisma = new PrismaClient();

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

  // Create default test admin
  const testHash = await bcrypt.hash('johndoe123', 12);
  await prisma.user.upsert({
    where: { email: 'john@doe.com' },
    update: {},
    create: {
      email: 'john@doe.com',
      name: 'Test Admin',
      password: testHash,
      role: 'ADMIN',
    },
  });

  // Create business admin user
  const adminHash = await bcrypt.hash('admin@', 12);
  await prisma.user.upsert({
    where: { email: 'admin@evotrucksales.com' },
    update: { password: adminHash },
    create: {
      email: 'admin@evotrucksales.com',
      name: 'Admin',
      password: adminHash,
      role: 'ADMIN',
    },
  });

  // Create main categories
  const categories = [
    { name: 'Camiones', nameEn: 'Trucks', slug: 'camiones', description: 'Camiones pesados y semi-remolques', imageUrl: '/images/cat-trucks.jpg', order: 1 },
    { name: 'Carros', nameEn: 'Cars', slug: 'carros', description: 'Vehículos comerciales y pickup trucks', imageUrl: '/images/cat-cars.jpg', order: 2 },
    { name: 'Retroexcavadoras', nameEn: 'Backhoes', slug: 'retroexcavadoras', description: 'Excavadoras y equipo de construcción', imageUrl: '/images/cat-excavators.jpg', order: 3 },
    { name: 'Dump Trucks', nameEn: 'Dump Trucks', slug: 'dump-trucks', description: 'Camiones de volteo', imageUrl: '/images/cat-dump.jpg', order: 4 },
    { name: 'Trailers', nameEn: 'Trailers', slug: 'trailers', description: 'Remolques y semi-remolques', imageUrl: '/images/cat-trailers.jpg', order: 5 },
    { name: 'Otros Equipos', nameEn: 'Other Equipment', slug: 'otros-equipos', description: 'Otros equipos pesados y maquinaria', imageUrl: '/images/cat-other.jpg', order: 6 },
  ];

  for (const cat of categories) {
    await prisma.category.upsert({
      where: { slug: cat.slug },
      update: { nameEn: cat.nameEn, imageUrl: cat.imageUrl },
      create: cat,
    });
  }

  // Get the Camiones category for subcategories
  const camionesCategory = await prisma.category.findUnique({ where: { slug: 'camiones' } });

  if (camionesCategory) {
    const truckSubcategories = [
      { name: 'Box Truck', nameEn: 'Box Truck', slug: 'box-truck', description: 'Camiones tipo caja', order: 10, parentId: camionesCategory.id },
      { name: 'Camiones Refrigerados', nameEn: 'Reefer Trucks', slug: 'reefer-trucks', description: 'Camiones refrigerados para transporte', order: 11, parentId: camionesCategory.id },
      { name: 'Flat Bed Trucks', nameEn: 'Flat Bed Trucks', slug: 'flat-bed-trucks', description: 'Camiones de plataforma plana', order: 12, parentId: camionesCategory.id },
      { name: 'Dump Trucks (Volteo)', nameEn: 'Dump Trucks', slug: 'dump-trucks-sub', description: 'Camiones de volteo', order: 13, parentId: camionesCategory.id },
      { name: 'Day Cab', nameEn: 'Day Cab', slug: 'day-cab', description: 'Camiones sin cabina dormitorio', order: 14, parentId: camionesCategory.id },
      { name: 'Sleeper Truck', nameEn: 'Sleeper Truck', slug: 'sleeper-truck', description: 'Camiones con cabina dormitorio', order: 15, parentId: camionesCategory.id },
    ];

    for (const sub of truckSubcategories) {
      await prisma.category.upsert({
        where: { slug: sub.slug },
        update: { nameEn: sub.nameEn, parentId: sub.parentId },
        create: sub,
      });
    }
  }

  console.log('Seed completed!');
}

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