name: QWED CI / CD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: qwed
          POSTGRES_PASSWORD: qwed_secret
          POSTGRES_DB: qwed_db
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.11"

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e ".[server,dev]"
        pip install pytest pytest-asyncio

    - name: Check Dependencies
      run: |
        python -c "import sqlglot; print('SQLGlot version:', sqlglot.__version__)"
        python -c "import qwed_new; print('Package qwed_new imported successfully')"
        python -c "import redis; r = redis.from_url('redis://localhost:6379'); print('Redis ping:', r.ping())"

    - name: Create .env file
      run: |
        echo "DATABASE_URL=postgresql://qwed:qwed_secret@localhost:5432/qwed_db" >> .env
        echo "REDIS_URL=redis://localhost:6379/0" >> .env
        echo "ACTIVE_PROVIDER=azure_openai" >> .env
        echo "AZURE_OPENAI_ENDPOINT=${{ secrets.AZURE_OPENAI_ENDPOINT }}" >> .env
        echo "AZURE_OPENAI_API_KEY=${{ secrets.AZURE_OPENAI_API_KEY }}" >> .env
        echo "AZURE_OPENAI_DEPLOYMENT=${{ secrets.AZURE_OPENAI_DEPLOYMENT }}" >> .env
        echo "AZURE_OPENAI_API_VERSION=2024-12-01-preview" >> .env

    - name: Start QWED API Server
      run: |
        # Start API server in background
        python -m qwed_sdk.cli &
        API_PID=$!
        echo "API_PID=$API_PID" >> $GITHUB_ENV
        
        # Wait for server to be ready (max 30 seconds)
        for i in {1..30}; do
          if curl -s http://localhost:8000/health > /dev/null 2>&1; then
            echo "API server is ready!"
            break
          fi
          echo "Waiting for API server... ($i/30)"
          sleep 1
        done
      continue-on-error: true  # Don't fail CI if API doesn't start

    - name: Run Tests with Coverage
      env:
        DATABASE_URL: postgresql://qwed:qwed_secret@localhost:5432/qwed_db
        REDIS_URL: redis://localhost:6379/0
        ACTIVE_PROVIDER: mock
      run: |
        pytest tests/ -v --cov=qwed_sdk --cov=src/qwed_new --cov-report=xml --cov-report=term

    - name: Upload Coverage to Codecov
      uses: codecov/codecov-action@v5
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: ./coverage.xml
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: false
