import { Payload } from 'payload';

export async function seedDatabase(payload: Payload) {
  // Check if any users exist
  const { totalDocs: totalUsers } = await payload.find({
    collection: 'users',
    limit: 1,
  });

  if (totalUsers === 0) {
    payload.logger.info('👤 No users found. Creating default admin user...');
    await payload.create({
      collection: 'users',
      data: {
        email: 'admin@mintel.me',
        password: 'klz-admin-setup',
      },
    });
    payload.logger.info('✅ Default admin user created successfully.');
  }

  // Check if any products exist
  const { totalDocs: totalProducts } = await payload.find({
    collection: 'products',
    limit: 1,
  });

  if (totalProducts === 0) {
    payload.logger.info('📦 No products found. Creating smoke test product (NAY2Y)...');
    await payload.create({
      collection: 'products',
      locale: 'de',
      data: {
        title: 'NAY2Y Smoke Test',
        sku: 'SMOKE-TEST-001',
        slug: 'nay2y',
        description: 'A dummy product for CI/CD smoke testing and OG image verification.',
        categories: [{ category: 'Power Cables' }],
        _status: 'published',
      },
    });
    payload.logger.info('✅ Smoke test product created successfully.');
  }
}
