#!/usr/bin/env nu

# Render Kubernetes Manifests from Nickel Templates
# Exports Nickel templates to YAML Kubernetes manifest format
# Usage: nu render-kubernetes.nu solo
# Usage: nu render-kubernetes.nu enterprise --namespace production

use ansi.nu
use external.nu
use paths.nu

export def main [
    mode: string                                    # Mode: solo | multiuser | cicd | enterprise
    --namespace: string = "provisioning"            # Kubernetes namespace (default: provisioning)
    --output-dir: string = ""                       # Output directory (default: infrastructure/kubernetes/{mode})
]: nothing -> nothing {
    # Validate inputs
    paths validate-mode $mode

    # Paths
    let default_output_dir: string = "provisioning/platform/infrastructure/kubernetes/" + $mode
    let output_path: string = if ($output_dir | str length) > 0 { $output_dir } else { $default_output_dir }

    print-k8s $"Rendering Kubernetes manifests for ($mode) mode"
    print $"  Namespace:   ($namespace)"
    print $"  Output Dir:  ($output_path)"
    print ""

    # Ensure output directory exists
    let _ = (paths ensure-dir $output_path)

    # List of Kubernetes manifests to render
    let manifests: list<string> = [
        "orchestrator-deployment.yaml.ncl"
        "orchestrator-service.yaml.ncl"
        "control-center-deployment.yaml.ncl"
        "control-center-service.yaml.ncl"
        "mcp-server-deployment.yaml.ncl"
        "platform-ingress.yaml.ncl"
    ]

    print-working $"Processing ($manifests | length) Kubernetes manifests..."
    print ""

    let templates_path: string = (paths templates-path)

    # For each manifest template, render and save
    for manifest in $manifests {
        let template_path: string = $templates_path + "/kubernetes/" + $manifest
        let output_file: string = ($manifest | str replace ".ncl" "")
        let output_file_path: string = $output_path + "/" + $output_file

        if not (external file-exists $template_path) {
            print-warning $"Template not found (will be created in Phase 9): ($manifest)"
            continue
        }

        print-working $"Rendering ($manifest)..."

        # Export Nickel template to JSON
        let json_result: record<exit_code: int, stdout: string, stderr: string> = (external run-cmd "nickel" ["export", "--format", "json", $template_path])

        if $json_result.exit_code != 0 {
            print-error $"Failed to export ($manifest): ($json_result.stderr)"
            continue
        }

        # Convert JSON to YAML
        let yaml_result: record<exit_code: int, stdout: string, stderr: string> = do {
            echo $json_result.stdout | ^yq -P
        } | complete

        if $yaml_result.exit_code != 0 {
            print-error $"Failed to convert ($manifest) to YAML: ($yaml_result.stderr)"
            continue
        }

        # Inject namespace if applicable
        let yaml_with_namespace: string = ($yaml_result.stdout | str replace "namespace: default" $"namespace: ($namespace)")

        # Save YAML
        (external pipe-to-file $yaml_with_namespace $output_file_path)

        print-success $"  → ($output_file)"
    }

    print ""
    print-success $"Kubernetes manifests rendered to: ($output_path)"

    print ""
    section "Next Steps"
    next-step "Validate:  kubectl apply -f ($output_path) --dry-run=client"
    next-step "Deploy:    kubectl apply -f ($output_path) --namespace ($namespace)"
    next-step "Status:    kubectl get deployments -n ($namespace)"
    next-step "Logs:      kubectl logs -n ($namespace) -l app=orchestrator -f"
}
