// cli_tool.cm — CLI tool simulation (macro benchmark)
// Parse args + process data + transform + format output.
// Simulates a realistic small utility program.

var json = use('json')

// Generate fake records
function generate_records(n) {
  var records = []
  var x = 42
  var i = 0
  var status_vals = ["active", "inactive", "pending", "archived"]
  var dept_vals = ["eng", "sales", "ops", "hr", "marketing"]
  for (i = 0; i < n; i++) {
    x = ((x * 1103515245 + 12345) & 0x7FFFFFFF) | 0
    push(records, {
      id: i + 1,
      name: `user_${i}`,
      score: (x % 1000) / 10,
      status: status_vals[i % 4],
      department: dept_vals[i % 5]
    })
  }
  return records
}

// Filter records by field value
function filter_records(records, field, value) {
  var result = []
  var i = 0
  for (i = 0; i < length(records); i++) {
    if (records[i][field] == value) {
      push(result, records[i])
    }
  }
  return result
}

// Group by a field
function group_by(records, field) {
  var groups = {}
  var i = 0
  var key = null
  for (i = 0; i < length(records); i++) {
    key = records[i][field]
    if (!key) key = "unknown"
    if (!groups[key]) groups[key] = []
    push(groups[key], records[i])
  }
  return groups
}

// Aggregate: compute stats per group
function aggregate(groups) {
  var keys = array(groups)
  var result = []
  var i = 0
  var j = 0
  var grp = null
  var total = 0
  var mn = 0
  var mx = 0
  for (i = 0; i < length(keys); i++) {
    grp = groups[keys[i]]
    total = 0
    mn = 999999
    mx = 0
    for (j = 0; j < length(grp); j++) {
      total += grp[j].score
      if (grp[j].score < mn) mn = grp[j].score
      if (grp[j].score > mx) mx = grp[j].score
    }
    push(result, {
      group: keys[i],
      count: length(grp),
      average: total / length(grp),
      low: mn,
      high: mx
    })
  }
  return result
}

// Full pipeline: load → filter → sort → group → aggregate → encode
function run_pipeline(n_records) {
  // Generate data
  var records = generate_records(n_records)

  // Filter to active records
  var filtered = filter_records(records, "status", "active")

  // Sort by score
  filtered = sort(filtered, "score")

  // Limit to first 50
  if (length(filtered) > 50) {
    filtered = array(filtered, 0, 50)
  }

  // Group and aggregate
  var groups = group_by(filtered, "department")
  var stats = aggregate(groups)
  stats = sort(stats, "average")

  // Encode as JSON
  var output = json.encode(stats)

  return length(output)
}

return {
  // Small dataset (100 records)
  cli_pipeline_100: function(n) {
    var i = 0
    var x = 0
    for (i = 0; i < n; i++) {
      x += run_pipeline(100)
    }
    return x
  },

  // Medium dataset (1000 records)
  cli_pipeline_1k: function(n) {
    var i = 0
    var x = 0
    for (i = 0; i < n; i++) {
      x += run_pipeline(1000)
    }
    return x
  },

  // Large dataset (10K records)
  cli_pipeline_10k: function(n) {
    var i = 0
    var x = 0
    for (i = 0; i < n; i++) {
      x += run_pipeline(10000)
    }
    return x
  }
}
