# Provisioning Project Detection Module
# Provides functions for technology detection and requirement inference

use ../../../lib_provisioning *

# Detect technologies in a project
export def detect-project [
  project_path: string
  --format: string = "text"
  --high-confidence-only: bool = false
  --pretty: bool = false
  --debug: bool = false
] {
  let detector_bin = (find-detector-binary)

  if ($detector_bin | is-empty) or not ($detector_bin | path exists) {
    return {
      error: "Detector binary not found"
      message: "Install: cargo build --release --bin provisioning-detector"
    }
  }

  mut args = [
    "detect"
    $project_path
    "--format" $format
  ]

  if $high_confidence_only {
    $args = ($args | append "--high-confidence-only")
  }

  if $pretty {
    $args = ($args | append "--pretty")
  }

  # Execute detector binary (no try-catch)
  let exec_result = (do { ^$detector_bin ...$args 2>&1 } | complete)
  if $exec_result.exit_code != 0 {
    return {
      error: "Detection failed"
      message: $exec_result.stderr
    }
  }

  let output = $exec_result.stdout
  if $format == "json" {
    $output | from json
  } else {
    { output: $output }
  }
}

# Analyze gaps in infrastructure declaration
export def complete-project [
  project_path: string
  --format: string = "text"
  --check: bool = false
  --pretty: bool = false
  --debug: bool = false
] {
  let detector_bin = (find-detector-binary)

  if ($detector_bin | is-empty) or not ($detector_bin | path exists) {
    return {
      error: "Detector binary not found"
      message: "Install: cargo build --release --bin provisioning-detector"
    }
  }

  mut args = [
    "complete"
    $project_path
    "--format" $format
  ]

  if $check {
    $args = ($args | append "--check")
  }

  if $pretty {
    $args = ($args | append "--pretty")
  }

  # Execute detector binary (no try-catch)
  let exec_result = (do { ^$detector_bin ...$args 2>&1 } | complete)
  if $exec_result.exit_code != 0 {
    return {
      error: "Completion failed"
      message: $exec_result.stderr
    }
  }

  let output = $exec_result.stdout
  if $format == "json" {
    $output | from json
  } else {
    { output: $output }
  }
}

# Find provisioning-detector binary in standard locations
def find-detector-binary [] {
  let possible_paths = [
    ($env.PROVISIONING? | default "" | path join "platform" "detector" "target" "release" "provisioning-detector")
    ($env.PROVISIONING? | default "" | path join "platform" "detector" "target" "debug" "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 {
    ""
  }
}

# Format detection results for display
export def format-detection [
  detection: record
  --format: string = "text"
] {
  if $format == "json" {
    $detection | to json
  } else {
    # Text formatting
    let output = @"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Project Technology Detection Results
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Overall Confidence: ($detection.overall_confidence | into float | math round -p 1 | into string)%

Detected Technologies:
"@

    let detections = if ($detection.detections | type) == "list" {
      $detection.detections
    } else {
      []
    }

    let result = if ($detections | length) > 0 {
      ($output + "\n" + ($detections | each {|d|
        $"  • ($d.technology) - ($d.confidence | into float | math round -p 1)%\n"
      } | str join ""))
    } else {
      $output + "\n  (No technologies detected)\n"
    }

    $result
  }
}

# Check if project has specific technology
export def has-technology [
  detection: record
  technology: string
] {
  $detection.detections
    | any {|d| ($d.technology | str downcase) == ($technology | str downcase) }
}

# Get all detected technologies
export def get-technologies [
  detection: record
] {
  $detection.detections | each {|d| $d.technology }
}

# Get all inferred requirements
export def get-requirements [
  detection: record
] {
  $detection.requirements | default []
}

# Get required taskservs only
export def get-required-taskservs [
  detection: record
] {
  ($detection.requirements | default [])
    | where {|r| $r.required == true }
    | each {|r| $r.taskserv }
}
