#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // 1. Define variables to hold the metrics
   uc.DefineVariable("request_count = 0");
   uc.DefineVariable("error_count = 0");
   uc.DefineVariable("total_response_time = 0.0");
   uc.DefineVariable("max_response_time = 0.0");

   // 2. Create the transformer and define the rule
   {
      uCalc::Transformer t(uc);
      t.Owned(); // Causes t to be released when it goes out of scope
      auto pattern = "{@String:request} {@Number:status} {@Number:time}ms";

      // 3. The replacement string uses @Exec for side-effects (updating variables)
      auto replacement = R"(
{@Exec: request_count++}
{@Exec: total_response_time = total_response_time + Double(time)}
{@Exec: max_response_time = Max(max_response_time, Double(time))}
{@Exec: iif(Double(status) >= 400, error_count++, 0)}
)";

      t.FromTo(pattern, replacement);

      // 4. Define the multi-line log data
      auto logText = R"(
2024-10-26 10:00:05 INFO    192.168.1.10   "GET /api/users HTTP/1.1" 200 15ms
2024-10-26 10:00:06 INFO    192.168.1.15   "GET /api/products HTTP/1.1" 200 22ms
2024-10-26 10:00:07 ERROR   192.168.1.22   "POST /api/login HTTP/1.1" 500 120ms
2024-10-26 10:00:08 INFO    192.168.1.10   "GET /api/users/1 HTTP/1.1" 200 8ms
)";

      // 5. Run the transformation (the output will be empty as we only use @Exec)
      t.Transform(logText);
   }

   // 6. Display the final aggregated metrics
   cout << "--- Log Analysis Summary ---" << endl;
   cout << "Total Requests: " << uc.EvalStr("request_count") << endl;
   cout << "Total Errors: " << uc.EvalStr("error_count") << endl;
   cout << "Average Response Time: " << uc.EvalStr("total_response_time / request_count") << "ms" << endl;
   cout << "Maximum Response Time: " << uc.EvalStr("max_response_time") << "ms" << endl;
}