#!/usr/bin/env nu
# Provisioning Workflow - Infrastructure-from-Code Pipeline

def main [
  path?: string                           # Project path
  --org: string = "default"               # Organization name
  --out (-o): string = "text"             # Output format
  --apply                                 # Apply recommendations
  --verbose (-v)                          # Enable verbose output
  --pretty                                # Pretty-print output
  --debug (-x)                            # Enable debug output
] {
  let project_path = ($path | default ".")

  # Validate path exists
  if not ($project_path | path exists) {
    print -e $"❌ Path not found: ($project_path)"
    return
  }

  if $debug {
    print $"🔍 Starting Infrastructure-from-Code workflow"
    print $"   Project: ($project_path)"
    print ""
  }

  # Run detection
  print ""
  print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  print "🔄 Infrastructure-from-Code Workflow"
  print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  print ""

  print "STEP 1: Technology Detection"
  print "────────────────────────────"

  let detector_bin = (detect-binary-path)

  if not ($detector_bin | path exists) {
    print -e "❌ Detector binary not found"
    return
  }

  let detect_result = (^$detector_bin detect $project_path --format json | complete)

  if $detect_result.exit_code != 0 {
    print -e "❌ Detection failed"
    return
  }

  let detection = ($detect_result.stdout | from json)
  if ($detection.detections? | default [] | is-not-empty) {
    print $"✓ Detected ($detection.detections | length) technologies"
  }
  print ""

  # Run completion
  print "STEP 2: Infrastructure Completion"
  print "─────────────────────────────────"

  let complete_result = (^$detector_bin complete $project_path --format json | complete)

  if $complete_result.exit_code != 0 {
    print -e "❌ Completion failed"
    return
  }

  let completion = ($complete_result.stdout | from json)
  if ($completion.completeness? | default null | is-not-empty) {
    let pct = ($completion.completeness | into float | math round -p 1 | into int)
    print $"✓ Completeness: ($pct)%"
  }
  print ""

  # Summary
  print "✅ Workflow Complete"
  print ""
}

# Helper: Locate the provisioning-detector binary
def detect-binary-path [] {
  let env_prov = ($env.PROVISIONING? | default "")

  let possible_paths = if ($env_prov | is-not-empty) {
    [
      ($env_prov | path join "platform" "target" "debug" "provisioning-detector")
      ($env_prov | path join "platform" "target" "release" "provisioning-detector")
      "/usr/local/bin/provisioning-detector"
      "/usr/bin/provisioning-detector"
    ]
  } else {
    [
      "/Users/Akasha/project-provisioning/provisioning/platform/target/debug/provisioning-detector"
      "/Users/Akasha/project-provisioning/provisioning/platform/target/release/provisioning-detector"
      "/usr/local/bin/provisioning-detector"
      "/usr/bin/provisioning-detector"
    ]
  }

  let existing = ($possible_paths | where { |p|
    (($p | is-not-empty) and ($p | path exists))
  })

  if ($existing | length) > 0 {
    $existing | first
  } else {
    if ($env_prov | is-not-empty) {
      $env_prov | path join "platform" "target" "release" "provisioning-detector"
    } else {
      "/Users/Akasha/project-provisioning/provisioning/platform/target/release/provisioning-detector"
    }
  }
}

export def "main workflow info" [] {
  print "Provisioning Workflow - Infrastructure-from-Code Pipeline"
}
