#!/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"

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

export def assert-dir-exists [path: string]: nothing -> nothing {
  let path_obj = $path | path expand

  if not ($path_obj | path exists) {
    error make {
      msg: $"Directory not found: ($path_obj)"
    }
  }

  if not ($path_obj | path type) == "dir" {
    error make {
      msg: $"Path exists but is not a directory: ($path_obj)"
    }
  }
}

export def ensure-dir [path: string]: string -> string {
  let expanded = $path | path expand

  if not ($expanded | path exists) {
    ^mkdir -p $expanded
  }

  $expanded
}

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

export def typedialog-base-path []: nothing -> string {
  "provisioning/.typedialog/platform"
}

export def schemas-base-path []: nothing -> string {
  "provisioning/schemas/platform"
}

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

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

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

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

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

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

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

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

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

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

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

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

export def get-template-path [template_name: string]: string -> string {
  (templates-path) + "/" + $template_name
}

export def get-output-config-path [service: string, mode: string]: string -> string {
  "provisioning/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 ', ')"
    }
  }
}
