# GitHub Actions Nickel Type Checking Workflow
# Generated by dev-system/ci
# Validates all Nickel schemas with nickel typecheck

name: Nickel Type Check

on:
  push:
    branches: [main, develop]
    paths: ['**.ncl']
  pull_request:
    branches: [main]
    paths: ['**.ncl']

jobs:
  typecheck:
    name: Nickel Type Checking
    runs-on: ubuntu-latest

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

      - name: Install Nickel
        run: |
          #!/usr/bin/env bash
          set -e

          echo "📦 Installing Nickel..."

          if command -v nickel &> /dev/null; then
            echo "✓ Nickel already installed"
            nickel --version
          else
            echo "Installing via homebrew..."
            brew install nickel || {
              echo "Homebrew installation failed, trying from source..."
              curl --proto '=https' --tlsv1.2 -sSf https://install.nickel-lang.org | bash || exit 1
            }
          fi

          nickel --version

      - name: Setup environment
        run: |
          #!/usr/bin/env bash
          # Set NICKEL_IMPORT_PATH for schema imports
          export NICKEL_IMPORT_PATH="/Users/Akasha/Tools/dev-system/ci/schemas:/Users/Akasha/Tools/dev-system/ci/validators:/Users/Akasha/Tools/dev-system/ci/defaults"
          echo "NICKEL_IMPORT_PATH=$NICKEL_IMPORT_PATH" >> $GITHUB_ENV

      - name: Type check schemas
        run: |
          #!/usr/bin/env bash
          set -e

          echo "🔍 Type checking Nickel schemas..."

          # Find all .ncl files
          SCHEMAS=$(find . -name "*.ncl" -type f \
            ! -path "./target/*" \
            ! -path "./.git/*" \
            ! -path "./node_modules/*" \
            | sort)

          if [ -z "$SCHEMAS" ]; then
            echo "⚠️  No Nickel schemas found"
            exit 0
          fi

          FAILED=0
          PASSED=0

          # Set import path
          export NICKEL_IMPORT_PATH="/Users/Akasha/Tools/dev-system/ci/schemas:/Users/Akasha/Tools/dev-system/ci/validators:/Users/Akasha/Tools/dev-system/ci/defaults:."

          for schema in $SCHEMAS; do
            echo "Checking: $schema"
            if nickel typecheck "$schema" > /dev/null 2>&1; then
              echo "  ✓ Valid"
              ((PASSED++))
            else
              echo "  ❌ Type error"
              nickel typecheck "$schema" || true
              ((FAILED++))
            fi
          done

          echo ""
          echo "Summary: $PASSED passed, $FAILED failed"

          if [ $FAILED -gt 0 ]; then
            exit 1
          fi

      - name: Export and validate
        run: |
          #!/usr/bin/env bash
          set -e

          echo "📊 Exporting Nickel configurations..."

          export NICKEL_IMPORT_PATH="/Users/Akasha/Tools/dev-system/ci/schemas:/Users/Akasha/Tools/dev-system/ci/validators:/Users/Akasha/Tools/dev-system/ci/defaults:."

          # Export main configs if they exist
          if [ -f ".typedialog/ci/schemas/ci-local.ncl" ]; then
            echo "Exporting CI config..."
            nickel export .typedialog/ci/schemas/ci-local.ncl > /tmp/ci-export.json
            if [ $? -eq 0 ]; then
              echo "  ✓ Successfully exported"
            else
              echo "  ❌ Export failed"
              exit 1
            fi
          fi

          echo "✓ All exports successful"
