#!/usr/bin/env nu

# VAPORA Provisioning Integration Validator
# Validates that Provisioning workspace is properly configured
# NOTE: Does NOT execute provisioning, only validates configuration

def main [] {
    print $"(ansi green)🔍 VAPORA Provisioning Integration Validator(ansi reset)"
    print $"(ansi blue)═══════════════════════════════════════════════(ansi reset)"
    print ""

    # Check if provisioning workspace exists
    if not ("provisioning/vapora-wrksp" | path exists) {
        print $"(ansi red)❌ provisioning/vapora-wrksp directory not found(ansi reset)"
        exit 1
    }

    print $"(ansi green)✅ Provisioning workspace exists(ansi reset)"

    # Check workspace.toml
    if ("provisioning/vapora-wrksp/workspace.toml" | path exists) {
        print $"(ansi green)✅ workspace.toml found(ansi reset)"
    } else {
        print $"(ansi yellow)⚠️  workspace.toml not found(ansi reset)"
    }

    # Validate KCL files
    print ""
    print $"(ansi yellow)📝 Checking KCL files...(ansi reset)"
    let kcl_files = (ls provisioning/vapora-wrksp/kcl/**/*.k | get name)

    if ($kcl_files | is-empty) {
        print $"(ansi yellow)⚠️  No KCL files found in provisioning/vapora-wrksp/kcl/(ansi reset)"
    } else {
        print $"(ansi green)✅ Found ($kcl_files | length) KCL file(s)(ansi reset)"
        for file in $kcl_files {
            let rel_path = ($file | str replace $"(pwd)/provisioning/vapora-wrksp/" "")
            print $"  • ($rel_path)"
        }
    }

    # Validate taskserv files
    print ""
    print $"(ansi yellow)🛠️  Checking taskserv definitions...(ansi reset)"
    let taskserv_files = (ls provisioning/vapora-wrksp/taskservs/**/*.toml 2>/dev/null | get name)

    if ($taskserv_files | is-empty) {
        print $"(ansi yellow)⚠️  No taskserv files found in provisioning/vapora-wrksp/taskservs/(ansi reset)"
    } else {
        print $"(ansi green)✅ Found ($taskserv_files | length) taskserv definition(s)(ansi reset)"
        for file in $taskserv_files {
            let rel_path = ($file | str replace $"(pwd)/provisioning/vapora-wrksp/" "")
            print $"  • ($rel_path)"
        }
    }

    # Validate workflow files
    print ""
    print $"(ansi yellow)🔄 Checking workflow definitions...(ansi reset)"
    let workflow_files = (ls provisioning/vapora-wrksp/workflows/**/*.yaml 2>/dev/null | get name)

    if ($workflow_files | is-empty) {
        print $"(ansi yellow)⚠️  No workflow files found in provisioning/vapora-wrksp/workflows/(ansi reset)"
    } else {
        print $"(ansi green)✅ Found ($workflow_files | length) workflow definition(s)(ansi reset)"
        for file in $workflow_files {
            let rel_path = ($file | str replace $"(pwd)/provisioning/vapora-wrksp/" "")
            print $"  • ($rel_path)"
        }
    }

    # Summary
    print ""
    print $"(ansi blue)═══════════════════════════════════════════════(ansi reset)"
    print $"(ansi green)✅ Provisioning integration validated(ansi reset)"
    print ""
    print $"(ansi yellow)📝 NOTE: Provisioning execution deferred for manual deployment(ansi reset)"
    print ""
    print $"(ansi cyan)To deploy using Provisioning:(ansi reset)"
    print "  1. cd provisioning/vapora-wrksp"
    print "  2. provisioning cluster create --config workspace.toml"
    print "  3. provisioning workflow run workflows/deploy-full-stack.yaml"
    print ""
    print $"(ansi cyan)For manual K8s deployment:(ansi reset)"
    print "  Use: nu scripts/deploy-k8s.nu"
}
