#!/usr/bin/env nu
# Path Management and Validation Helpers
# Provides consistent path handling for Phase 8 scripts
# Usage: use paths.nu; assert-file-exists "/some/file"

# Calculate project root from this script's location
# This script is in: provisioning/.typedialog/platform/scripts/
# Project root is: provisioning/
def project-root []: nothing -> string {
  $env.FILE_PWD | path dirname | path dirname | path dirname
}

export def assert-file-exists [path: string] {
  if not ($path | path exists) {
    error make {
      msg: $"File not found: ($path)"
    }
  }
}

export def assert-dir-exists [path: string] {
  let path_obj = $path | path expand
  if not ($path_obj | path exists) {
    error make {
      msg: $"Directory not found: ($path_obj)"
    }
  }
  if ($path_obj | path type) != "dir" {
    error make {
      msg: $"Path exists but is not a directory: ($path_obj)"
    }
  }
}

export def ensure-dir [path: string]: nothing -> string {
  let expanded = $path | path expand
  if not ($expanded | path exists) {
    mkdir $expanded
  }
  $expanded
}

export def resolve-relative [path: string]: nothing -> string {
  if ($path | str starts-with "/") {
    $path
  } else if ($path | str starts-with "~/") {
    $path | path expand
  } else {
    $env.PWD | path join $path | path expand
  }
}

export def typedialog-base-path []: nothing -> string {
  (project-root) | path join ".typedialog" "platform"
}

export def schemas-base-path []: nothing -> string {
  (project-root) | path join "schemas" "platform"
}

export def forms-path []: nothing -> string {
  (typedialog-base-path) | path join "forms"
}

export def fragments-path []: nothing -> string {
  (forms-path) | path join "fragments"
}

export def schemas-path []: nothing -> string {
  (schemas-base-path) | path join "schemas"
}

export def defaults-path []: nothing -> string {
  (schemas-base-path) | path join "defaults"
}

export def validators-path []: nothing -> string {
  (schemas-base-path) | path join "validators"
}

export def configs-path []: nothing -> string {
  (schemas-base-path) | path join "configs"
}

export def templates-path []: nothing -> string {
  (schemas-base-path) | path join "templates"
}

export def values-path []: nothing -> string {
  (schemas-base-path) | path join "values"
}

export def constraints-path []: nothing -> string {
  (schemas-base-path) | path join "constraints"
}

export def get-form-path [service: string]: nothing -> string {
  (forms-path) | path join $"($service)-form.toml"
}

export def get-config-path [service: string, mode: string]: nothing -> string {
  (configs-path) | path join $"($service).($mode).ncl"
}

export def get-value-path [service: string, mode: string]: nothing -> string {
  (values-path) | path join $"($service).($mode).ncl"
}

export def get-template-path [template_name: string]: nothing -> string {
  (templates-path) | path join $template_name
}

export def get-output-config-path [service: string, mode: string]: nothing -> string {
  (project-root) | path join "config" "runtime" "generated" $"($service).($mode).toml"
}

export def validate-service [service: string]: nothing -> nothing {
  let valid_services = ["orchestrator", "control-center", "mcp-server", "vault-service", "extension-registry", "rag", "ai-service", "provisioning-daemon"]
  if $service not-in $valid_services {
    error make {
      msg: $"Invalid service: ($service). Valid options: ($valid_services | str join ', ')"
    }
  }
}

export def validate-mode [mode: string]: nothing -> nothing {
  let valid_modes = ["solo", "multiuser", "cicd", "enterprise"]
  if $mode not-in $valid_modes {
    error make {
      msg: $"Invalid deployment mode: ($mode). Valid options: ($valid_modes | str join ', ')"
    }
  }
}

export def validate-backend [backend: string]: nothing -> nothing {
  let valid_backends = ["cli", "tui", "web"]
  if $backend not-in $valid_backends {
    error make {
      msg: $"Invalid TypeDialog backend: ($backend). Valid options: ($valid_backends | str join ', ')"
    }
  }
}
