#!/usr/bin/env nu

use toolkit.nu fmt

fmt # --check --verbose

# ADR-025: Block root star-imports of lib_provisioning / main_provisioning.
# A line matching `use lib_provisioning *` or `use main_provisioning *` at the
# start of a file (top-level) reintroduces the transitive parse cost this
# refactor was designed to eliminate. All imports must be selective.
let staged = (git diff --cached --name-only | lines | where { str ends-with ".nu" })

if ($staged | length) > 0 {
    let violations = (
        $staged
        | each {|f|
            let hits = (
                do { git show $":($f)" } | complete
                | if $in.exit_code == 0 { $in.stdout } else { "" }
                | lines
                | enumerate
                | where { $it.item | str starts-with "use lib_provisioning *" or $it.item | str starts-with "use main_provisioning *" }
                | each {|row| $"  ($f):($row.index + 1): ($row.item | str trim)"}
            )
            $hits
        }
        | flatten
    )

    if ($violations | length) > 0 {
        print "❌ ADR-025 star-import violation — selective imports required:"
        for v in $violations { print $v }
        print ""
        print "Replace `use lib_provisioning *` with explicit `use lib_provisioning/path/to/module.nu [sym1 sym2]`"
        exit 1
    }
}
