#!/usr/bin/env nu
# Convert JSON to Nickel configuration with schema imports (Nushell)
# Usage: ./json-to-nickel.nu <input.json> [output.ncl]

def main [
    input: path,      # Input JSON file
    output?: path     # Output Nickel file (optional)
] {
    let output_path = if ($output == null) {
        $"($input | path parse | get stem).ncl"
    } else {
        $output
    }

    if not ($input | path exists) {
        error make {msg: $"Input file not found: ($input)"}
    }

    let json_content = (open $input | to json)
    let timestamp = (date now | format date "%Y-%m-%dT%H:%M:%SZ")

    let nickel_content = $"# Generated Nickel configuration from JSON
# Source: ($input)
# Generated at: ($timestamp)

let schemas = import \"../schemas/config.ncl\" in
let defaults = import \"../defaults/config.ncl\" in

# User-provided values
let user_values = ($json_content) in

# Merge with defaults
let final_config = std.record.merge defaults user_values in

final_config | schemas.Config"

    $nickel_content | save --force $output_path
    print $"Converted: ($output_path)"
    print $"Validate with: nickel typecheck ($output_path)"
}
