#!/usr/bin/env nu
# Export Nickel configuration to TOML format
#
# Usage:
#   ./export-toml.nu <service> [mode] [output-dir]
#   ./export-toml.nu orchestrator solo
#   ./export-toml.nu orchestrator solo ./custom-output
#   ./export-toml.nu orchestrator  # Uses PROVISIONING_MODE env var
#
# Examples:
#   # Export from examples
#   ./export-toml.nu orchestrator solo
#   # Output: provisioning/config/runtime/generated/orchestrator.solo.toml
#
#   # Export from custom location
#   ./export-toml.nu orchestrator solo ./workspace/config/runtime
#   # Output: ./workspace/config/runtime/generated/orchestrator.solo.toml

use std

# Default paths relative to this script
let script_dir = (pwd)
let project_root = $script_dir | path dirname | path dirname | path dirname | path dirname
let provisioning = $env.PROVISIONING? // ($project_root / "provisioning")

# Parse arguments
let service = ($in | get 0)
let mode = ($in | get 1?)? // ($env.PROVISIONING_MODE? // "solo")
let output_base = ($in | get 2?)? // ($provisioning / "config" / "runtime")

# Validate service name
let valid_services = [
  "orchestrator"
  "control-center"
  "vault-service"
  "mcp-server"
  "installer"
  "extension-registry"
  "rag"
  "ai-service"
]

if ($service not-in $valid_services) {
  print $"Error: Invalid service '$service'"
  print $"Valid services: ($valid_services | str join ', ')"
  exit 1
}

# Validate mode
let valid_modes = ["solo" "multiuser" "cicd" "enterprise"]
if ($mode not-in $valid_modes) {
  print $"Error: Invalid mode '$mode'"
  print $"Valid modes: ($valid_modes | str join ', ')"
  exit 1
}

# Input file: config/runtime/{service}.{mode}.ncl
let input_file = $provisioning / "config" / "runtime" / $"($service).($mode).ncl"

# Ensure output directory exists
let output_dir = $output_base / "generated"
mkdir -p $output_dir

# Output file: {output_dir}/{service}.{mode}.toml
let output_file = $output_dir / $"($service).($mode).toml"

# Check if input file exists
if not ($input_file | path exists) {
  print $"Error: Input file not found: $input_file"
  print ""
  print "Available options:"
  print "1. Copy example and customize:"
  print $"   cp ($provisioning)/config/examples/($service).($mode).example.ncl ($input_file)"
  print $"   # Then customize as needed"
  print ""
  print "2. Or use generate-configs.nu to create from defaults"
  exit 1
}

# Export NCL to TOML
print $"Exporting: ($input_file) → ($output_file)"

# Set NICKEL_IMPORT_PATH for import resolution
let nickel_path = if ($env.NICKEL_IMPORT_PATH? == "") {
  $"($provisioning):."
} else {
  $env.NICKEL_IMPORT_PATH
}

try {
  # Export to TOML
  let export_result = (
    with-env { NICKEL_IMPORT_PATH: $nickel_path } {
      nickel export --format toml $input_file
    }
  )

  # Write to output file
  $export_result | save --raw $output_file

  print $"✅ Success: Exported to ($output_file)"
  print ""
  print "Config summary:"
  print $"  Service: ($service)"
  print $"  Mode: ($mode)"
  print $"  Source: ($input_file)"
  print $"  Output: ($output_file)"
  print $"  Size: (($output_file | stat).size | into string) bytes"
} catch { |err|
  print $"❌ Error: Failed to export TOML"
  print $"Error details: ($err.msg)"
  exit 1
}
