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

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();

            // Then seed Canvas app/system
            const canvasAppSeeder = new CanvasAppSeed(fork);
            await canvasAppSeeder.run();

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