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

const prisma = new PrismaClient();

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

  // Create default admin user
  const hashedPassword = await bcrypt.hash('Admin@123', 10);

  const admin = await prisma.user.upsert({
    where: { email: 'admin@motopv.com' },
    update: {},
    create: {
      email: 'admin@motopv.com',
      passwordHash: hashedPassword,
      name: 'Admin User',
      role: 'SUPER_ADMIN',
      isActive: true,
    },
  });

  console.log('✅ Admin user created:', admin.email);
  console.log('📧 Email: admin@motopv.com');
  console.log('🔑 Password: Admin@123');
  console.log('⚠️  Please change this password after first login!');

  // Optionally create some sample quotes for testing
  if (process.env.NODE_ENV === 'development') {
    const sampleQuote = await prisma.quote.create({
      data: {
        customerName: 'John Doe',
        email: 'john.doe@example.com',
        phone: '+265991234567',
        address: 'Blantyre, Malawi',
        propertyType: 'RESIDENTIAL',
        energyConsumption: 500,
        systemSize: 5,
        budgetRange: '$5,000 - $10,000',
        message: 'Interested in solar panels for my home',
        source: 'website',
        status: 'PENDING',
        priority: 'MEDIUM',
      },
    });

    console.log('✅ Sample quote created:', sampleQuote.id);
  }

  console.log('✨ Database seed completed!');
}

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