const {
    init,
    version,
    list_available_fluids,
    WasmSystem,
    WasmPressure,
    WasmTemperature,
    WasmFallbackConfig,
    WasmCompressor,
    WasmCondenser,
    WasmEvaporator,
    WasmExpansionValve
} = require('../pkg/entropyk_wasm.js');

async function main() {
    console.log('Entropyk WASM Integration Test');
    console.log('==============================');

    // Initialize module
    await init();
    console.log('WASM Version:', version());

    // Verify fluids
    const fluids = list_available_fluids();
    console.log('Available fluids:', fluids);
    if (!fluids.includes('R134a')) {
        throw new Error('R134a should be available');
    }

    // Create system
    const system = new WasmSystem();
    console.log('System created');

    // Create components with correct API signatures
    const compressor = new WasmCompressor(
        "R134a",    // fluid
        2900.0,     // speed_rpm
        0.0001,     // displacement_m3_per_rev
        0.85,       // efficiency
        0.85, 2.5, 500.0, 1500.0, -2.5,  // AHRI m1-m5
        1.8, 600.0, 1600.0, -3.0, 2.0    // AHRI m6-m10
    ).into_component();

    const condenser = new WasmCondenser(5000.0).into_component();
    const evaporator = new WasmEvaporator(3000.0).into_component();
    const valve = new WasmExpansionValve("R134a", 0.0).into_component();

    const cIdx = system.add_component(compressor);
    const condIdx = system.add_component(condenser);
    const eIdx = system.add_component(evaporator);
    const vIdx = system.add_component(valve);

    console.log(`Added 4 components. Node count: ${system.node_count()}`);

    // Connect components
    system.add_edge(cIdx, condIdx);
    system.add_edge(condIdx, vIdx);
    system.add_edge(vIdx, eIdx);
    system.add_edge(eIdx, cIdx);

    console.log(`Connected components. Edge count: ${system.edge_count()}`);

    // Finalize system
    system.finalize();
    console.log('System finalized');

    // Solve
    const config = new WasmFallbackConfig();
    console.log('Solving system...');
    const result = system.solve(config);

    console.log('Solve Result:', result.toJson());

    if (result.converged) {
        console.log('Convergence achieved in', result.iterations, 'iterations');
        console.log('Status:', result.status);

        // Extract result for a node
        const state = system.get_node_result(0);
        console.log('Node 0 state:', state.toJson());
    } else {
        console.error('System did not converge, status:', result.status);
    }

    // Test input validation
    console.log('\n--- Input validation tests ---');
    try { new WasmPressure(-1.0); console.error('FAIL: negative pressure accepted'); } catch(e) { console.log('OK: negative pressure rejected'); }
    try { new WasmTemperature(-1.0); console.error('FAIL: negative temperature accepted'); } catch(e) { console.log('OK: negative temperature rejected'); }
    try { new WasmCondenser(-100.0); console.error('FAIL: negative UA accepted'); } catch(e) { console.log('OK: negative UA rejected'); }
    try { new WasmCondenser(0.0); console.error('FAIL: zero UA accepted'); } catch(e) { console.log('OK: zero UA rejected'); }

    console.log('\nWASM Integration Test PASSED (API verification complete)');
}

main().catch(err => {
    console.error('Test failed:', err);
    process.exit(1);
});
