const assert = require('assert');
const mikrotik = require('./services/mikrotikService');

// Mock axios to avoid actual network calls
mikrotik.execute = async (method, url) => {
    if (url.includes('/interface')) {
        return []; // No interfaces found
    }
    if (url.includes('/ppp/secret?name=valid_user')) {
        return [{ name: 'valid_user', disabled: 'false' }]; // User exists in secrets
    }
    if (url.includes('/ppp/secret?name=invalid_user')) {
        return []; // User does not exist
    }
    return [];
};

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

// Helper to mock Gemini tool call logic without full API
async function testLogic() {
    console.log("Testing Logic: valid_user (offline)...");

    // Simulate what gemini.js does inside the tool handler
    // 1. Valid Offline User
    const call1 = { args: { name: 'valid_user' } };
    let result1;

    // Manual execution of the logic added to gemini.js
    const found1 = await mikrotik.findInterface(call1.args.name);
    if (found1) {
        result1 = "Should not be found";
    } else {
        const secretName = call1.args.name.replace(/^pppoe-/, '');
        const secret = await mikrotik.getPppoeSecret(secretName);
        if (secret) {
            result1 = `User '${secretName}' is registered (Valid) but currently OFFLINE (No active session detected).`;
        } else {
            result1 = `Interface/User '${call1.args.name}' not found.`;
        }
    }

    console.log("Result 1:", result1);
    assert.strictEqual(result1, "User 'valid_user' is registered (Valid) but currently OFFLINE (No active session detected).");

    // 2. Invalid User
    console.log("Testing Logic: invalid_user...");
    const call2 = { args: { name: 'invalid_user' } };
    let result2;

    const found2 = await mikrotik.findInterface(call2.args.name);
    if (found2) {
        result2 = "Should not be found";
    } else {
        const secretName = call2.args.name.replace(/^pppoe-/, '');
        const secret = await mikrotik.getPppoeSecret(secretName);
        if (secret) {
            result2 = "Should not be found here";
        } else {
            result2 = `Interface/User '${call2.args.name}' not found. Please check the name or username.`;
        }
    }

    console.log("Result 2:", result2);
    assert.ok(result2.includes("not found"), "Should return not found error");

    console.log("\nTEST PASSED: Logic distinguishes between Offline and Invalid users.");
}

testLogic();
