const mysql = require('mysql2/promise');
require('dotenv').config();

async function resetData() {
    const config = {
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        port: parseInt(process.env.DB_PORT) || 3307
    };

    console.log(`🚀 Connecting to ${config.host}:${config.port} as ${config.user}...`);

    let connection;
    try {
        connection = await mysql.createConnection(config);

        // 1. Reset SmartIMS (sokuree_platform_dev)
        console.log('--- Resetting SmartIMS (sokuree_platform_dev) ---');
        await connection.query('USE sokuree_platform_dev');

        const [updateResult] = await connection.query(`
            UPDATE system_modules 
            SET is_active = false, 
                license_key = NULL, 
                license_type = NULL, 
                expiry_date = NULL, 
                subscriber_id = NULL
        `);
        console.log(`✅ Deactivated All Modules in system_modules (${updateResult.affectedRows} rows)`);

        await connection.query('TRUNCATE TABLE license_history');
        console.log('✅ Truncated license_history in SmartIMS');


        // 2. Reset License Manager (smart_ims_license_db)
        console.log('\n--- Resetting License Manager (smart_ims_license_db) ---');
        await connection.query('USE smart_ims_license_db');

        await connection.query('TRUNCATE TABLE issued_licenses');
        console.log('✅ Truncated issued_licenses in License Manager');

        await connection.query('TRUNCATE TABLE license_history');
        console.log('✅ Truncated license_history in License Manager');

        console.log('\n✨ Database reset completed successfully!');

    } catch (err) {
        console.error('\n❌ FAILED to reset data:', err.message);
    } finally {
        if (connection) await connection.end();
    }
}

resetData();
