#!/usr/bin/env nu

# Test all Phase 9 templates (Docker Compose, Kubernetes, Configs)
# Validates Nickel syntax and exports to JSON/YAML/TOML
#
# Usage: nu test-phase9-templates.nu [--output-dir ./test-output]

export def main [
    --output_dir: string = "./test-output"
]: nothing -> nothing {
    let green = (ansi green)
    let red = (ansi red)
    let yellow = (ansi yellow)
    let blue = (ansi blue)
    let reset = (ansi reset)

    print $"($blue)╔════════════════════════════════════════════════════════╗($reset)"
    print $"($blue)║     Phase 9: Template Testing & Validation Suite      ║($reset)"
    print $"($blue)╚════════════════════════════════════════════════════════╝($reset)"
    print ""

    # Create output directory
    do { ^mkdir -p $output_dir } | complete | ignore

    # ========================================================================
    # Phase 9.A: Docker Compose Templates
    # ========================================================================
    print $"($yellow)Phase 9.A: Docker Compose Templates($reset)"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    [
        "platform-stack.solo.yml.ncl",
        "platform-stack.multiuser.yml.ncl",
        "platform-stack.cicd.yml.ncl",
        "platform-stack.enterprise.yml.ncl",
    ] | each { |template|
        let template_path = $"provisioning/schemas/platform/templates/docker-compose/($template)"
        let output_path = $"($output_dir)/($template | str replace '.ncl' '.json')"

        let test_result = do {
            ^nickel export --format json $template_path | save --force $output_path
        } | complete

        if $test_result.exit_code == 0 {
            print $"  ($green)✓($reset) ($template)"
        } else {
            print $"  ($red)✗($reset) ($template)"
        }
    }

    print ""

    # ========================================================================
    # Phase 9.B: Kubernetes Deployment Templates
    # ========================================================================
    print $"($yellow)Phase 9.B: Kubernetes Deployment Templates($reset)"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    [
        "orchestrator-deployment.yaml.ncl",
        "orchestrator-service.yaml.ncl",
        "control-center-deployment.yaml.ncl",
        "control-center-service.yaml.ncl",
        "mcp-server-deployment.yaml.ncl",
        "mcp-server-service.yaml.ncl",
        "platform-ingress.yaml.ncl",
    ] | each { |template|
        let template_path = $"provisioning/schemas/platform/templates/kubernetes/($template)"
        let output_path = $"($output_dir)/($template | str replace '.ncl' '.yaml')"

        let test_result = do {
            ^nickel export --format json $template_path | ^yq -P | save --force $output_path
        } | complete

        if $test_result.exit_code == 0 {
            print $"  ($green)✓($reset) ($template)"
        } else {
            print $"  ($red)✗($reset) ($template)"
        }
    }

    print ""

    # ========================================================================
    # Phase 9.C: Kubernetes Namespace/Configuration Templates
    # ========================================================================
    print $"($yellow)Phase 9.C: Kubernetes Namespace & Configuration($reset)"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    [
        "namespace.yaml.ncl",
        "resource-quota.yaml.ncl",
        "network-policy.yaml.ncl",
        "rbac.yaml.ncl",
    ] | each { |template|
        let template_path = $"provisioning/schemas/platform/templates/kubernetes/($template)"
        let output_path = $"($output_dir)/($template | str replace '.ncl' '.yaml')"

        let test_result = do {
            ^nickel export --format json $template_path | ^yq -P | save --force $output_path
        } | complete

        if $test_result.exit_code == 0 {
            print $"  ($green)✓($reset) ($template)"
        } else {
            print $"  ($red)✗($reset) ($template)"
        }
    }

    print ""

    # ========================================================================
    # Phase 9.D: Service Configuration Templates
    # ========================================================================
    print $"($yellow)Phase 9.D: Service Configuration Templates (TOML)($reset)"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    [
        "orchestrator-config.toml.ncl",
        "control-center-config.toml.ncl",
        "mcp-server-config.toml.ncl",
    ] | each { |template|
        let template_path = $"provisioning/schemas/platform/templates/configs/($template)"
        let output_path = $"($output_dir)/($template | str replace '.ncl' '')"

        let test_result = do {
            ^nickel export --format toml $template_path | save --force $output_path
        } | complete

        if $test_result.exit_code == 0 {
            print $"  ($green)✓($reset) ($template)"
        } else {
            print $"  ($red)✗($reset) ($template)"
        }
    }

    print ""

    # ========================================================================
    # Test Summary
    # ========================================================================
    print $"($blue)═══════════════════════════════════════════════════════($reset)"
    print $"($green)✓ Phase 9 Template Testing Complete!($reset)"
    print ""
    print $"Generated files in: ($output_dir)"
    print ""
    print "Files created:"
    print "  - Docker Compose: 4 templates (solo, multiuser, cicd, enterprise)"
    print "  - Kubernetes: 11 templates (deployments, services, ingress, namespace, RBAC, policies)"
    print "  - Service Configs: 3 templates (TOML format)"
    print ""
}
