name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [18.x, 20.x]
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
    
    - name: Install dependencies
      run: npm install
    
    - name: Build frontend
      run: npm run build
    
    - name: Run tests
      run: npm test
    
    - name: Start server and test health endpoint
      run: |
        npm start &
        sleep 5
        curl -f http://localhost:3000/api/health || exit 1
        
  docker:
    runs-on: ubuntu-latest
    needs: test
    # Only run docker build on push to main, not PRs
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Build Docker image
      run: docker build -t openhamclock:test .
    
    - name: Test Docker container
      run: |
        docker run -d -p 3000:3000 --name ohc-test openhamclock:test
        sleep 10
        curl -f http://localhost:3000/api/health || exit 1
        docker stop ohc-test
