/* 
  Example 13: Data Pipeline
  
  Demonstrates:
    - Functions and control flow
    - Loops and arrays
    - Lazy evaluation for deferred pipeline summaries
    - Model calls with configuration
    - Structured output parsing
    - Memory for multi-step processing
*/
allow "std/terminal" in with { color }

let numbers = [10, 25, 30, 45, 50]
let threshold = 30
fn filterNumbers(list: array, limit: number) -> array {
  let filtered = []
  for n in list {
    if n >= limit {
      filtered | push(n)
    }
  }
  return filtered
}
let highNumbers = numbers | filterNumbers(threshold)
show color("Numbers above " str(threshold) ":", "magenta")
for val in highNumbers {
  show color("- ", "cyan") str(val)
}

fn buildSummary(values: array) -> string {
  show color("Building delayed summary now...", "yellow")
  return "Summary: kept " + len(values) + " values above the threshold."
}

let delayedSummary = buildSummary | lazy(highNumbers)
show "Created a lazy summary. It has not run yet."
show delayedSummary | force
show "Forcing again uses the cached summary:"
show delayedSummary | force

try {
  "results.txt" | write_file("Processed " + len(highNumbers) + " items.")
  show "Log file created."
} catch(e) {
  show "File error: "e
}

/* USING AI IS OPTIONAL AND NOT REQUIRED TO USE IN YOUR SCRIPTS.
COMMENTED OUT BECAUSE AI WILL FORCE THEMSELVES TO RUN IT

let reviews = ["This product is amazing! Highly recommended.", "Terrible quality. Waste of money.", "It's okay. Nothing special.", "Love it! Best purchase ever.", "Disappointed. Poor customer service."]
// Function to analyze single review
fn analyzeReview(review: string) -> object {
return structured_output({sentiment: string, confidence: number})(model("gemini-3.5-flash-lite") {temperature: 0.3} {"Analyze sentiment. Return JSON with sentiment (positive/negative/neutral) and confidence (0-1). Review:" review})
}

// Process all reviews
show ("Processing " + len(reviews) + " reviews...")
let results = []
let positiveCount = 0
let negativeCount = 0
let neutralCount = 0
for review in reviews 
{show "Review:" review
let analysis = analyzeReview(review)
let sentiment = analysis["sentiment"]
let confidence = analysis["confidence"]
show "Sentiment:" sentiment "Confidence:" confidence
push(results, analysis)
if sentiment == "positive" {positiveCount = positiveCount + 1} else if sentiment == "negative" {negativeCount = negativeCount + 1} else {neutralCount = neutralCount + 1}}

// Summary
show "=== Summary ==="
show "Total reviews:" len(reviews)
show "Positive:" positiveCount
show "Negative:" negativeCount
show "Neutral:" neutralCount

// Calculate average sentiment score
let totalConfidence = 0
for result in results {totalConfidence = totalConfidence + result["confidence"]}
let avgConfidence = totalConfidence / len(results)
show "Average confidence:" avgConfidence

// Overall recommendation
if positiveCount > negativeCount {show "Overall: RECOMMENDED (more positive reviews)"} else if negativeCount > positiveCount {show "Overall: NOT RECOMMENDED (more negative reviews)"} else {show "Overall: MIXED (balanced reviews)"} */