# Parameterized Blocks Example
# Demonstrates reusable blocks with arguments for DRY workflows

# Simple parameterized block
block research(topic):
  session "Research {topic} thoroughly"
  session "Summarize key findings about {topic}"
  session "List open questions about {topic}"

# Invoke with different arguments
do research("quantum computing")
do research("machine learning")
do research("blockchain technology")

# Block with multiple parameters
block review_code(language, focus_area):
  session "Review the {language} code for {focus_area} issues"
  session "Suggest {focus_area} improvements for {language}"
  session "Provide {language} best practices for {focus_area}"

do review_code("Python", "performance")
do review_code("TypeScript", "type safety")
do review_code("Rust", "memory safety")

# Parameterized block for data processing
block process_dataset(source, format):
  session "Load data from {source}"
  session "Validate {format} structure"
  session "Transform to standard format"
  session "Generate quality report for {source} data"

do process_dataset("sales_db", "CSV")
do process_dataset("api_logs", "JSON")
do process_dataset("user_events", "Parquet")

# Blocks with parameters used in control flow
block test_feature(feature_name, test_level):
  session "Write {test_level} tests for {feature_name}"

  if **the tests reveal issues**:
    session "Fix issues in {feature_name}"
    session "Re-run {test_level} tests for {feature_name}"
  else:
    session "Mark {feature_name} {test_level} testing complete"

do test_feature("authentication", "unit")
do test_feature("payment processing", "integration")
do test_feature("user dashboard", "e2e")

# Parameterized blocks in parallel
block analyze_competitor(company):
  session "Research {company} products"
  session "Analyze {company} market position"
  session "Identify {company} strengths and weaknesses"

parallel:
  a = do analyze_competitor("Company A")
  b = do analyze_competitor("Company B")
  c = do analyze_competitor("Company C")

session "Create competitive analysis report"
  context: { a, b, c }

# Block with error handling
block safe_api_call(endpoint, method):
  try:
    session "Call {endpoint} with {method} request"
      retry: 3
      backoff: "exponential"
  catch as err:
    session "Log failed {method} call to {endpoint}"
      context: err
    session "Return fallback response for {endpoint}"

do safe_api_call("/users", "GET")
do safe_api_call("/orders", "POST")
do safe_api_call("/inventory", "PUT")

# Nested block invocations
block full_review(component):
  do review_code("TypeScript", "security")
  do test_feature(component, "unit")
  session "Generate documentation for {component}"

do full_review("UserService")
do full_review("PaymentGateway")

# Block with loop inside
block process_batch(batch_name, items):
  session "Start processing {batch_name}"
  for item in items:
    session "Process item from {batch_name}"
      context: item
  session "Complete {batch_name} processing"

let batch1 = ["a", "b", "c"]
let batch2 = ["x", "y", "z"]

do process_batch("alpha", batch1)
do process_batch("beta", batch2)
