const db = require('../db');

async function createCompleteAcademyStructure() {
    try {
        console.log('🚀 Creating Complete SOC Analyst Academy Structure\n');

        // Paths already created, continuing with modules...
        console.log('Creating Path 1 expansion modules...\n');

        // Module 3: Windows Fundamentals
        await db.query(
            'INSERT INTO modules (learning_path_id, title, description, estimated_hours) VALUES (?, ?, ?, ?)',
            [1, 'Windows Fundamentals', 'Master Windows OS, Active Directory basics, and PowerShell for security operations.', 16]
        );
        const module3Id = (await db.query('SELECT LAST_INSERT_ID() as id')).rows[0].id;

        const winTasks = [
            'Windows File System', 'User Account Control', 'Windows Registry',
            'Active Directory Intro', 'PowerShell Fundamentals', 'Windows Event Logs',
            'Group Policy', 'Windows Security Tools'
        ];
        for (let i = 0; i < winTasks.length; i++) {
            await db.query('INSERT INTO tasks (module_id, title, display_order, xp_reward) VALUES (?, ?, ?, ?)',
                [module3Id, winTasks[i], i + 1, 25]);
        }
        console.log(`✓ Module 3: Windows Fundamentals (8 tasks)`);

        // Module 5: Security Principles  
        await db.query(
            'INSERT INTO modules (learning_path_id, title, description, estimated_hours) VALUES (?, ?, ?, ?)',
            [1, 'Security Principles', 'Core security concepts: CIA Triad, AAA, encryption, defense in depth.', 14]
        );
        const module5Id = (await db.query('SELECT LAST_INSERT_ID() as id')).rows[0].id;

        const secTasks = [
            'CIA Triad', 'Authentication Methods', 'Authorization', 'Accounting',
            'Encryption Basics', 'Hashing vs Encryption', 'Defense in Depth'
        ];
        for (let i = 0; i < secTasks.length; i++) {
            await db.query('INSERT INTO tasks (module_id, title, display_order, xp_reward) VALUES (?, ?, ?, ?)',
                [module5Id, secTasks[i], i + 1, 25]);
        }
        console.log(`✓ Module 5: Security Principles (7 tasks)`);

        // Module 6: Command Line Mastery
        await db.query(
            'INSERT INTO modules (learning_path_id, title, description, estimated_hours) VALUES (?, ?, ?, ?)',
            [1, 'Command Line Mastery', 'Advanced command line with grep, sed, awk, and text processing.', 14]
        );
        const module6Id = (await db.query('SELECT LAST_INSERT_ID() as id')).rows[0].id;

        const cmdTasks = [
            'Advanced grep', 'sed Basics', 'awk Programming', 'Regular Expressions',
            'Bash Scripting', 'Pipes & Redirection', 'CLI Efficiency'
        ];
        for (let i = 0; i < cmdTasks.length; i++) {
            await db.query('INSERT INTO tasks (module_id, title, display_order, xp_reward) VALUES (?, ?, ?, ?)',
                [module6Id, cmdTasks[i], i + 1, 25]);
        }
        console.log(`✓ Module 6: Command Line Mastery (7 tasks)\n`);

        console.log('✅ Path 1 expansion complete!\n');
        console.log('Creating Path 2 modules...\n');

        // Path 2 modules (simplified for now)
        const path2Modules = [
            { title: 'Log Analysis Fundamentals', tasks: 8 },
            { title: 'Wireshark & PCAP Analysis', tasks: 10 },
            { title: 'Understanding Proxies', tasks: 5 },
            { title: 'Introduction to IDS/IPS', tasks: 7 },
            { title: 'Vulnerability Scanning', tasks: 8 },
            { title: 'Web Application Scanning', tasks: 7 },
            { title: 'Threat Intelligence Basics', tasks: 8 },
            { title: 'Email Analysis', tasks: 7 },
            { title: 'Social Engineering Defense', tasks: 6 },
            { title: 'Virtualization & Lab Setup', tasks: 9 }
        ];

        for (const mod of path2Modules) {
            await db.query(
                'INSERT INTO modules (learning_path_id, title, description, estimated_hours) VALUES (?, ?, ?, ?)',
                [2, mod.title, `${mod.title} for SOC operations`, 15]
            );
            const modId = (await db.query('SELECT LAST_INSERT_ID() as id')).rows[0].id;

            for (let i = 1; i <= mod.tasks; i++) {
                await db.query('INSERT INTO tasks (module_id, title, display_order, xp_reward) VALUES (?, ?, ?, ?)',
                    [modId, `${mod.title} - Task ${i}`, i, 30]);
            }
            console.log(`✓ ${mod.title} (${mod.tasks} tasks)`);
        }

        console.log('\n✅ Path 2 complete! (75 tasks)\n');
        console.log('Database structure creation complete!\n');

        const counts = await db.query('SELECT COUNT(*) as count FROM tasks');
        console.log(`Total tasks in database: ${counts.rows[0].count}`);

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

createCompleteAcademyStructure();
