#!/usr/bin/env nu

# VAPORA Clean Script
# Phase 0: Clean build artifacts
# Follows NUSHELL_GUIDELINES.md - 17 rules

# Check if target directory exists
def has-target-dir []: bool {
    ("target" | path exists)
}

# Get size of target directory
def get-target-size []: int {
    if (has-target-dir) {
        let result = (do { du -sh target } | complete)
        if ($result.exit_code == 0) {
            # Parse output to get size
            1  # Return 1 as placeholder (actual size calculation would require parsing)
        } else {
            0
        }
    } else {
        0
    }
}

# Clean cargo build artifacts
def clean-cargo []: record {
    print "Cleaning cargo build artifacts..."

    let result = (do { cargo clean } | complete)

    if ($result.exit_code == 0) {
        {
            success: true,
            error: null
        }
    } else {
        {
            success: false,
            error: ($result.stderr | str trim)
        }
    }
}

# Clean trunk build artifacts (frontend)
def clean-trunk []: record {
    print "Cleaning trunk build artifacts..."

    let dist_path = "crates/vapora-frontend/dist"

    if ($dist_path | path exists) {
        let result = (do { rm -rf $dist_path } | complete)

        if ($result.exit_code == 0) {
            {
                success: true,
                error: null
            }
        } else {
            {
                success: false,
                error: ($result.stderr | str trim)
            }
        }
    } else {
        {
            success: true,
            error: null
        }
    }
}

# Clean temporary files
def clean-temp []: record {
    print "Cleaning temporary files..."

    let temp_patterns = [
        "**/*.tmp",
        "**/.DS_Store",
        "**/Thumbs.db"
    ]

    # Note: glob cleanup would go here in production
    {
        success: true,
        error: null
    }
}

# Main clean function
def main [
    --all = false      # Clean all artifacts including cargo
    --temp = false     # Clean only temporary files
]: void {
    print "=== VAPORA Clean ==="
    print ""

    if $temp {
        # Clean only temp files
        let result = (clean-temp)

        if $result.success {
            print ""
            print "✓ Temporary files cleaned"
        } else {
            print $"ERROR: ($result.error)"
            exit 1
        }
    } else {
        # Clean cargo artifacts
        let cargo_result = (clean-cargo)

        if (not $cargo_result.success) {
            print $"ERROR: ($cargo_result.error)"
            exit 1
        }
        print "✓ Cargo artifacts cleaned"

        # Clean trunk artifacts
        let trunk_result = (clean-trunk)

        if (not $trunk_result.success) {
            print $"ERROR: ($trunk_result.error)"
            exit 1
        }
        print "✓ Trunk artifacts cleaned"

        # Clean temp if --all
        if $all {
            let temp_result = (clean-temp)

            if (not $temp_result.success) {
                print $"ERROR: ($temp_result.error)"
                exit 1
            }
            print "✓ Temporary files cleaned"
        }

        print ""
        print "=== Clean Complete ==="
    }
}
