#!/usr/bin/env nu

# Fix circular dependencies in foundation crates
def main [] {
    print "🔧 Fixing circular dependencies in foundation crates..."

    let foundation_crates = ["client", "server", "core-lib", "core-types", "components", "pages", "tools", "utils"]

    for $crate_name in $foundation_crates {
        fix_crate_deps $crate_name
    }

    print "✅ All circular dependencies fixed!"
}

def fix_crate_deps [crate_name: string] {
    let crate_path = $"foundation/crates/($crate_name)"
    let cargo_toml = $"($crate_path)/Cargo.toml"

    if not ($cargo_toml | path exists) {
        print $"  ⚠️  ($cargo_toml) not found, skipping..."
        return
    }

    print $"  🔧 Fixing ($crate_name)..."

    # Read the Cargo.toml file
    let content = (open $cargo_toml)

    # Remove self-dependencies
    let fixed_content = (
        $content
        | reject --ignore-errors dependencies.($crate_name)
        | reject --ignore-errors build-dependencies.($crate_name)
        | reject --ignore-errors dev-dependencies.($crate_name)
    )

    # Save the fixed Cargo.toml
    $fixed_content | to toml | save --force $cargo_toml

    print $"    ✓ Fixed ($crate_name)"
}
