// Run this script to create learning path tables
const db = require('../db');
const fs = require('fs');
const path = require('path');

const runMigration = async () => {
    try {
        console.log('🔄 Creating Learning Paths tables...');

        const sqlPath = path.join(__dirname, 'create_learning_paths_tables.sql');
        const sql = fs.readFileSync(sqlPath, 'utf8');

        // Split by semicolon and execute each statement
        const statements = sql.split(';').filter(stmt => stmt.trim().length > 0);

        for (const statement of statements) {
            await db.query(statement);
        }

        console.log('✅ Learning Paths tables created successfully!');

        console.log('🔄 Seeding initial data...');
        const seedPath = path.join(__dirname, 'seed_learning_paths.sql');
        const seedSql = fs.readFileSync(seedPath, 'utf8');

        const seedStatements = seedSql.split(';').filter(stmt => stmt.trim().length > 0);

        for (const statement of seedStatements) {
            await db.query(statement);
        }

        console.log('✅ Seed data inserted successfully!');
        console.log('🎉 Learning Paths feature is ready!');

        process.exit(0);
    } catch (error) {
        console.error('❌ Migration error:', error);
        process.exit(1);
    }
};

runMigration();
