import { NestFactory } from '@nestjs/core';
import { AppModule } from '../src/app.module';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Category } from '../src/modules/entities';

async function bootstrap() {
  const app = await NestFactory.createApplicationContext(AppModule);
  
  try {
    const categoryRepository = app.get(getRepositoryToken(Category));
    
    // Define categories with Macedonian names and English slugs
    const categories = [
      {
        name: 'Спорт',
        slug: 'sport',
        description: 'Спортски вести и анализи',
        order: 1,
      },
      {
        name: 'Уметност',
        slug: 'art',
        description: 'Уметност, култура и забава',
        order: 2,
      },
      {
        name: 'Наука',
        slug: 'science',
        description: 'Научни откритија и технологија',
        order: 3,
      },
    ];

    console.log('Seeding categories...');
    
    for (const categoryData of categories) {
      // Check if category already exists
      const existingCategory = await categoryRepository.findOne({
        where: { slug: categoryData.slug },
      });

      if (existingCategory) {
        console.log(`Category "${categoryData.name}" (${categoryData.slug}) already exists`);
      } else {
        const category = categoryRepository.create(categoryData);
        await categoryRepository.save(category);
        console.log(`Created category: "${categoryData.name}" (${categoryData.slug})`);
      }
    }

    console.log('\n✅ Category seeding completed!');
    console.log('Categories available:');
    const allCategories = await categoryRepository.find({ order: { order: 'ASC' } });
    allCategories.forEach((cat: Category) => {
      console.log(`  • ${cat.name} (${cat.slug}) - ${cat.description || 'No description'}`);
    });

  } catch (error) {
    console.error('Error seeding categories:', error);
  } finally {
    await app.close();
  }
}

bootstrap();