name: Provisioning Tests

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'scripts/provisioning/**'
      - 'configs/installation.toml'
      - 'tests/provisioning/**'
      - '.github/workflows/test-provisioning.yml'
  pull_request:
    branches: [ main, develop ]
    paths:
      - 'scripts/provisioning/**'
      - 'configs/installation.toml'
      - 'tests/provisioning/**'
      - '.github/workflows/test-provisioning.yml'

env:
  RUST_BACKTRACE: 1

jobs:
  provisioning-tests:
    name: Provisioning Tests (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, ubuntu-20.04]
      fail-fast: false

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install NuShell
        run: |
          if [[ "${{ runner.os }}" == "Linux" ]]; then
            curl -o nu https://releases.nushell.dev/latest && chmod +x nu && sudo mv nu /usr/local/bin/
          else
            brew install nushell
          fi

      - name: Verify NuShell installation
        run: nu --version

      - name: Run all provisioning tests
        run: |
          echo "Running provisioning test suite..."
          nu tests/provisioning/test-all.nu
        continue-on-error: false

      - name: Test preset detection
        run: |
          echo "Testing preset detection..."
          nu scripts/provisioning/install-with-presets.nu --list-presets

      - name: Test provctl detection
        run: |
          echo "Testing provctl detection..."
          nu scripts/provisioning/detect-provctl.nu --verbose || true

      - name: Validate configuration files
        run: |
          echo "Validating configuration files..."
          test -f configs/installation.toml && echo "✓ installation.toml found"
          test -f configs/provisioning/services/surrealdb.toml && echo "✓ surrealdb.toml found"
          test -f configs/provisioning/services/nats.toml && echo "✓ nats.toml found"
          test -f configs/provisioning/services/syntaxis-api.toml && echo "✓ syntaxis-api.toml found"

      - name: Generate sample configuration
        run: |
          echo "Generating sample configurations for each preset..."
          for preset in minimal local dev staging production custom; do
            echo "---"
            echo "Generating config for preset: $preset"
            nu scripts/provisioning/install-with-presets.nu \
              --preset $preset \
              --generate-config > /tmp/config-$preset.toml 2>&1 || true
            if [ -f "/tmp/config-$preset.toml" ]; then
              echo "✓ Generated config for $preset"
            fi
          done

  lint-provisioning:
    name: Lint Provisioning Scripts
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install NuShell
        run: |
          curl -o nu https://releases.nushell.dev/latest && chmod +x nu && sudo mv nu /usr/local/bin/

      - name: Check NuShell syntax
        run: |
          echo "Checking NuShell script syntax..."
          for script in scripts/provisioning/*.nu tests/provisioning/*.nu; do
            echo "Checking: $script"
            nu -c "source $script; def main { }; main" || true
          done

      - name: Validate TOML syntax
        run: |
          if command -v toml-cli &> /dev/null; then
            echo "Validating TOML files..."
            for toml in configs/installation.toml configs/provisioning/services/*.toml; do
              echo "Validating: $toml"
              toml-cli validate "$toml" || true
            done
          else
            echo "toml-cli not available, skipping TOML validation"
          fi

  report:
    name: Test Report
    runs-on: ubuntu-latest
    needs: [provisioning-tests, lint-provisioning]
    if: always()

    steps:
      - name: Print test summary
        run: |
          echo "Provisioning Test Summary"
          echo "========================="
          if [[ "${{ needs.provisioning-tests.result }}" == "success" ]]; then
            echo "✅ Provisioning tests: PASSED"
          else
            echo "❌ Provisioning tests: FAILED"
          fi

          if [[ "${{ needs.lint-provisioning.result }}" == "success" ]]; then
            echo "✅ Linting: PASSED"
          else
            echo "❌ Linting: FAILED"
          fi

      - name: Determine final status
        run: |
          if [[ "${{ needs.provisioning-tests.result }}" != "success" ]] || \
             [[ "${{ needs.lint-provisioning.result }}" != "success" ]]; then
            echo "❌ Some tests failed"
            exit 1
          else
            echo "✅ All provisioning tests passed!"
          fi
