name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: jobs_scraper_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
      
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - 5672:5672
          - 15672:15672

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.24'
    
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18'
    
    - name: Cache Go modules
      uses: actions/cache@v3
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    
    - name: Install dependencies
      run: |
        go mod download
        cd services/scraper-glassdoor && npm ci
    
    - name: Run tests - API Service
      run: |
        cd services/api
        go test ./...
    
    - name: Run tests - LinkedIn Scraper
      run: |
        cd services/scraper-linkedin
        go test ./...
    
    - name: Run tests - Cron Analyzer
      run: |
        cd apps/cron-analyzer
        go test ./...
    
    - name: Run tests - CV Analyzer
      run: |
        cd apps/cv-analyzer
        go test ./...
    
    - name: Build all services
      run: |
        make build-api
        make build-scraper-linkedin
        make build-scraper-glassdoor
        make build-cron-analyzer
        make build-cv-analyzer

  docker:
    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 Docker Hub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    
    - name: Build and push API service
      uses: docker/build-push-action@v5
      with:
        context: ./services/api
        push: true
        tags: ${{ secrets.DOCKER_USERNAME }}/jobs-scraper-api:latest
    
    - name: Build and push LinkedIn scraper
      uses: docker/build-push-action@v5
      with:
        context: ./services/scraper-linkedin
        push: true
        tags: ${{ secrets.DOCKER_USERNAME }}/jobs-scraper-linkedin:latest
