#!/usr/bin/env nu

# Version Consistency Checker
# Ensures nushell system version matches submodule version

# Get nushell version from submodule Cargo.toml
def get_submodule_version [] {
    let nushell_cargo = "nushell/Cargo.toml"
    if not ($nushell_cargo | path exists) {
        error make {msg: "Nushell submodule not found. Please run 'git submodule update --init --recursive'"}
    }

    try {
        let content = open $nushell_cargo
        $content.package.version
    } catch {
        error make {msg: "Could not read nushell version from submodule"}
    }
}

# Get installed nushell version
def get_system_version [] {
    try {
        nu --version | str trim
    } catch {
        error make {msg: "Could not get system nushell version"}
    }
}

# Check if versions match
def check_versions [] {
    let submodule_version = get_submodule_version
    let system_version = get_system_version

    {
        submodule: $submodule_version,
        system: $system_version,
        match: ($submodule_version == $system_version)
    }
}

# Main function for version checking
def main [
    --fix                    # Attempt to fix version mismatch
    --quiet                  # Suppress output except errors
] {
    let versions = check_versions

    if not $quiet {
        print $"🔍 Version Check:"
        print $"  Submodule: ($versions.submodule)"
        print $"  System:    ($versions.system)"
    }

    if $versions.match {
        if not $quiet {
            print "✅ Versions match - OK"
        }
        return true
    } else {
        print "❌ Version mismatch detected!"
        print $"  Expected: ($versions.submodule) - from submodule"
        print $"  Found:    ($versions.system) - system nushell"
        print ""

        if $fix {
            print "🔧 Attempting to fix version mismatch..."
            print "💡 Running update_nushell.sh to install correct version..."

            try {
                # Call the update_nushell.sh script to fix the installation
                let result = run-external "bash" ["scripts/sh/update_nushell.sh", "update"]
                print "✅ Nushell updated successfully"
                return true
            } catch {
                print "❌ Failed to update nushell automatically"
                print "🛠️  Manual fix required:"
                print $"   1. Run: ./scripts/sh/update_nushell.sh update"
                print $"   2. Or install nushell ($versions.submodule) manually"
                return false
            }
        } else {
            print "🛠️  To fix this issue:"
            print "   1. Run: nu scripts/nu/check_version.nu --fix"
            print "   2. Or manually: ./scripts/sh/update_nushell.sh update"
            return false
        }
    }
}

# Export function for use by other scripts
export def version_check [--quiet] {
    let versions = check_versions
    if not $versions.match {
        if not $quiet {
            print $"❌ Version mismatch: system=($versions.system), required=($versions.submodule)"
        }
        return false
    }
    return true
}