/**
 * Test: Latency measurement for HIL systems
 * 
 * Measures round-trip latency for solve operations.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include "entropyk.h"

#define NUM_ITERATIONS 100
#define TARGET_LATENCY_MS 20.0

int main() {
    printf("Test: HIL Latency\n");
    printf("  Target: < %.1f ms per solve\n", TARGET_LATENCY_MS);
    
    /* Setup: Create a simple system */
    EntropykSystem* sys = entropyk_system_create();
    assert(sys != NULL);
    
    double coeffs[10] = {0.85, 2.5, 500.0, 1500.0, -2.5, 1.8, 600.0, 1600.0, -3.0, 2.0};
    EntropykComponent* comp = entropyk_compressor_create(coeffs, 10);
    EntropykComponent* cond = entropyk_condenser_create(5000.0);
    EntropykComponent* valve = entropyk_expansion_valve_create();
    EntropykComponent* evap = entropyk_evaporator_create(3000.0);
    
    assert(comp && cond && valve && evap);
    
    unsigned int comp_idx = entropyk_system_add_component(sys, comp);
    unsigned int cond_idx = entropyk_system_add_component(sys, cond);
    unsigned int valve_idx = entropyk_system_add_component(sys, valve);
    unsigned int evap_idx = entropyk_system_add_component(sys, evap);
    
    assert(comp_idx != UINT32_MAX);
    assert(cond_idx != UINT32_MAX);
    assert(valve_idx != UINT32_MAX);
    assert(evap_idx != UINT32_MAX);
    
    entropyk_system_add_edge(sys, comp_idx, cond_idx);
    entropyk_system_add_edge(sys, cond_idx, valve_idx);
    entropyk_system_add_edge(sys, valve_idx, evap_idx);
    entropyk_system_add_edge(sys, evap_idx, comp_idx);
    
    entropyk_system_finalize(sys);
    
    EntropykFallbackConfig config = {
        .newton = {50, 1e-4, false, 1000},
        .picard = {200, 1e-3, 0.5}
    };
    
    /* Measure latency */
    printf("  Running %d solve iterations...\n", NUM_ITERATIONS);
    
    double total_ms = 0.0;
    int success_count = 0;
    
    for (int i = 0; i < NUM_ITERATIONS; i++) {
        EntropykSolverResult* result = NULL;
        
        clock_t start = clock();
        EntropykErrorCode err = entropyk_solve_fallback(sys, &config, &result);
        clock_t end = clock();
        
        double elapsed_ms = 1000.0 * (end - start) / CLOCKS_PER_SEC;
        total_ms += elapsed_ms;
        
        if (err == ENTROPYK_OK) {
            success_count++;
            entropyk_result_free(result);
        }
    }
    
    double avg_ms = total_ms / NUM_ITERATIONS;
    
    printf("  Results:\n");
    printf("    Total time: %.2f ms\n", total_ms);
    printf("    Average latency: %.3f ms\n", avg_ms);
    printf("    Successful solves: %d / %d\n", success_count, NUM_ITERATIONS);
    
    /* Note: Stub components don't actually solve, so latency should be very fast */
    printf("    Average latency: %.3f ms %s target of %.1f ms\n",
           avg_ms,
           avg_ms < TARGET_LATENCY_MS ? "<" : ">=",
           TARGET_LATENCY_MS);
    
    entropyk_system_free(sys);
    
    printf("Latency test PASSED\n");
    return 0;
}
