import { EntityManager } from '@mikro-orm/core';
import { UserSeed } from './UserSeed';
import { RoleSeed } from './RoleSeed';

export class DatabaseSeeder {
    constructor(private readonly em: EntityManager) {}

    async run() {
        console.log('Starting database seeding...');

        const fork = this.em.fork();

        try {
            // Seed roles first since users depend on them
            const roleSeeder = new RoleSeed(fork);
            await roleSeeder.run();

            // Then seed users
            const userSeeder = new UserSeed(fork);
            await userSeeder.run();

            await fork.flush();
            console.log('Database seeding completed!');
        } catch (error) {
            console.error('Error during seeding:', error);
            throw error;
        }
    }
}
