#!/usr/bin/env bash
# Test the complete bootstrap installation flow
# This tests the recipe system, download, and installation

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NIP_DIR="$(dirname "$SCRIPT_DIR")"
TEST_DIR="$NIP_DIR/test-bootstrap"

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "NIP Bootstrap Flow Test"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Clean up any previous test
if [ -d "$TEST_DIR" ]; then
    echo "Cleaning up previous test..."
    rm -rf "$TEST_DIR"
fi

mkdir -p "$TEST_DIR"

# Set up test environment
export HOME="$TEST_DIR/home"
export XDG_DATA_HOME="$TEST_DIR/data"
export XDG_CACHE_HOME="$TEST_DIR/cache"
export XDG_CONFIG_HOME="$TEST_DIR/config"

mkdir -p "$HOME" "$XDG_DATA_HOME" "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME"

echo "Test environment:"
echo "  HOME: $HOME"
echo "  DATA: $XDG_DATA_HOME"
echo "  CACHE: $XDG_CACHE_HOME"
echo ""

# Find nip binary
if [ -f "$NIP_DIR/src/nip.out" ]; then
    NIP="$NIP_DIR/src/nip.out"
elif [ -f "$NIP_DIR/src/nip" ]; then
    NIP="$NIP_DIR/src/nip"
else
    echo "Error: nip binary not found"
    echo "Please compile nip first: cd nip && nim c src/nip.nim"
    exit 1
fi

echo "Using nip: $NIP"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test 1: List recipes"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

$NIP bootstrap recipes || {
    echo "✗ Failed to list recipes"
    exit 1
}

echo ""
echo "✓ Test 1 passed"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test 2: Validate recipes"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

for tool in nix pkgsrc gentoo; do
    echo "Validating $tool..."
    $NIP bootstrap validate $tool || {
        echo "✗ Failed to validate $tool recipe"
        exit 1
    }
    echo ""
done

echo "✓ Test 2 passed"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test 3: Show tool info"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

$NIP bootstrap info nix || {
    echo "✗ Failed to show nix info"
    exit 1
}

echo ""
echo "✓ Test 3 passed"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test 4: List installed tools (should be empty)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

$NIP bootstrap list || {
    echo "✗ Failed to list tools"
    exit 1
}

echo ""
echo "✓ Test 4 passed"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Test 5: Update recipes"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

$NIP bootstrap update-recipes || {
    echo "✗ Failed to update recipes"
    exit 1
}

echo ""
echo "✓ Test 5 passed"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "All tests passed!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Test environment preserved at: $TEST_DIR"
echo "To clean up: rm -rf $TEST_DIR"
echo ""
echo "⚠️  Note: Installation test skipped (requires actual binaries)"
echo "   To test installation, use real binaries and run:"
echo "   $NIP bootstrap install nix"
