name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:        
        node: [20, 22, 24]
        firebird: [3, 4, 5]

    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 10

    - name: Setup FirebirdSQL ${{ matrix.firebird }} container
      run: |
        # Create data directory
        sudo mkdir -p /firebird/data
        sudo chmod 755 /firebird/data
        
        # Start Firebird container
        docker run -d \
          --name firebird \
          -e FIREBIRD_ROOT_PASSWORD="masterkey" \
          -e FIREBIRD_CONF_WireCrypt="Enabled" \
          -e FIREBIRD_CONF_AuthServer="Legacy_Auth;Srp;Win_Sspi" \
          -p 3050:3050 \
          -v /firebird/data:/firebird/data \
          firebirdsql/firebird:${{ matrix.firebird }}
        
        # Wait for Firebird to be ready
        MAX_RETRIES=30
        RETRY_INTERVAL=2
        echo "Waiting for Firebird to start..."
        FIREBIRD_READY=false
        for i in $(seq 1 $MAX_RETRIES); do
          if docker exec firebird /opt/firebird/bin/isql -user SYSDBA -password masterkey -z 2>&1 | grep -q "ISQL Version"; then
            echo "Firebird is ready!"
            FIREBIRD_READY=true
            break
          fi
          echo "Waiting... ($i/$MAX_RETRIES)"
          sleep $RETRY_INTERVAL
        done
        
        if [ "$FIREBIRD_READY" = false ]; then
          echo "ERROR: Firebird failed to start within the timeout period"
          docker ps -a
          docker logs firebird
          exit 1
        fi
        
        # Verify Firebird is running
        docker ps -a
        docker logs firebird

    - name: Use Node.js ${{ matrix.node }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node }}


    - name: Build
      shell: bash
      run: |
        npm ci         

    - name: Test (Linux)      
      run: |
        export FIREBIRD_DATA=/firebird/data
        npm test
