name: Stream Processor CI/CD

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

env:
  PYTHON_VERSION: '3.11'
  NODE_VERSION: '18'

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_stream_db
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
      
      redis:
        image: redis:7-alpine
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 6379:6379
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: ${{ env.PYTHON_VERSION }}
    
    - name: Install system dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y ffmpeg postgresql-client
    
    - name: Install Python dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r src/requirements.txt
        pip install pytest-cov
    
    - name: Set up environment variables
      run: |
        echo "SECRET_KEY=test-secret-key-for-ci" >> $GITHUB_ENV
        echo "DEBUG=True" >> $GITHUB_ENV
        echo "DB_NAME=test_stream_db" >> $GITHUB_ENV
        echo "DB_USER=postgres" >> $GITHUB_ENV
        echo "DB_PASSWORD=postgres" >> $GITHUB_ENV
        echo "DB_HOST=localhost" >> $GITHUB_ENV
        echo "DB_PORT=5432" >> $GITHUB_ENV
        echo "CELERY_BROKER_URL=redis://localhost:6379/0" >> $GITHUB_ENV
        echo "CELERY_RESULT_BACKEND=redis://localhost:6379/0" >> $GITHUB_ENV
        echo "REDIS_URL=redis://localhost:6379/1" >> $GITHUB_ENV
    
    - name: Run database migrations
      run: |
        cd src
        python manage.py migrate
    
    - name: Run tests with coverage
      run: |
        cd src
        python -m pytest --cov=apps --cov-report=xml --cov-report=html --tb=short -v
    
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        file: ./src/coverage.xml
        fail_ci_if_error: true
    
    - name: Run linting
      run: |
        cd src
        python -m flake8 apps/ --max-line-length=100 --exclude=migrations
        python -m black --check apps/
    
    - name: Run security checks
      run: |
        cd src
        python -m bandit -r apps/ -f json -o bandit-report.json || true
        python -m safety check

  build:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Login to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ secrets.DOCKER_REGISTRY }}
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    
    - name: Build and push web image
      uses: docker/build-push-action@v5
      with:
        context: .
        file: ./docker/web.Dockerfile
        push: true
        tags: |
          ${{ secrets.DOCKER_REGISTRY }}/stream-processor:latest
          ${{ secrets.DOCKER_REGISTRY }}/stream-processor:${{ github.sha }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
    
    - name: Build and push worker image
      uses: docker/build-push-action@v5
      with:
        context: .
        file: ./docker/worker.Dockerfile
        push: true
        tags: |
          ${{ secrets.DOCKER_REGISTRY }}/stream-processor-worker:latest
          ${{ secrets.DOCKER_REGISTRY }}/stream-processor-worker:${{ github.sha }}
        cache-from: type=gha
        cache-to: type=gha,mode=max

  deploy-staging:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    environment: staging
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Deploy to staging
      run: |
        echo "Deploying to staging environment..."
        # Add your deployment commands here
        # Examples:
        # - kubectl apply -f k8s/staging/
        # - docker-compose -f docker-compose.staging.yml up -d
        # - ansible-playbook deploy-staging.yml
    
    - name: Run health checks
      run: |
        echo "Running health checks..."
        # Add health check commands here
        # curl -f http://staging.yourdomain.com/health/

  deploy-production:
    needs: deploy-staging
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    environment: production
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Deploy to production
      run: |
        echo "Deploying to production environment..."
        # Add your production deployment commands here
    
    - name: Run production health checks
      run: |
        echo "Running production health checks..."
        # Add production health check commands here
    
    - name: Notify deployment success
      if: success()
      run: |
        echo "Deployment successful! Sending notifications..."
        # Add notification logic here (Slack, email, etc.)
    
    - name: Rollback on failure
      if: failure()
      run: |
        echo "Deployment failed! Initiating rollback..."
        # Add rollback logic here
