## Example: Profile Resolution Orchestrator
##
## This example demonstrates profiling the complete resolution pipeline
## to identify performance bottlenecks.

import ../src/nip/resolver/profiler
import ../src/nip/resolver/orchestrator
import ../src/nip/resolver/types
import ../src/nip/cas/storage
import times

proc main() =
  echo ""
  echo "=" .repeat(80)
  echo "PROFILING RESOLUTION ORCHESTRATOR"
  echo "=" .repeat(80)
  echo ""
  
  # Enable profiler
  enableProfiler()
  
  # Create orchestrator
  let cas = newCASStorage("/tmp/nip-profile-cas")
  let repos: seq[Repository] = @[]  # Empty for now
  let config = defaultConfig()
  
  let orchestrator = newResolutionOrchestrator(cas, repos, config)
  
  echo "Running resolution operations..."
  echo ""
  
  # Simulate multiple resolutions
  for i in 0..<10:
    echo fmt"Resolution {i + 1}/10"
    
    let result = orchestrator.resolve(
      fmt"package-{i}",
      ">=1.0.0",
      VariantDemand(
        useFlags: @["ssl", "http2"],
        libc: "musl",
        allocator: "jemalloc",
        targetArch: "x86_64",
        buildFlags: @[]
      )
    )
    
    if result.isOk:
      let res = result.get
      echo fmt"  ✓ Resolved {res.packageCount} packages in {res.resolutionTime:.3f}s (cache hit: {res.cacheHit})"
    else:
      echo "  ✗ Resolution failed"
  
  # Disable profiler
  disableProfiler()
  
  echo ""
  echo "Profiling complete!"
  echo ""
  
  # Print report
  printReport()
  printOptimizationRecommendations()
  
  # Export to CSV
  exportToCSV("orchestrator_profiling.csv")
  exportDetailedToCSV("orchestrator_profiling_detailed.csv")
  
  echo ""
  echo "=" .repeat(80)
  echo "PROFILING COMPLETE"
  echo "=" .repeat(80)
  echo ""
  echo "Results exported to:"
  echo "  - orchestrator_profiling.csv (summary)"
  echo "  - orchestrator_profiling_detailed.csv (detailed timings)"
  echo ""
  
  # Print metrics
  echo "Orchestrator Metrics:"
  echo fmt"  Total resolutions: {orchestrator.metrics.totalResolutions}"
  echo fmt"  Successful: {orchestrator.metrics.successfulResolutions}"
  echo fmt"  Cache hits: {orchestrator.metrics.cacheHits}"
  echo fmt"  Cache misses: {orchestrator.metrics.cacheMisses}"
  echo fmt"  Total time: {orchestrator.metrics.totalTime:.3f}s"
  echo ""

when isMainModule:
  main()
