name: CI

on:
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up JDK 25
        uses: actions/setup-java@v4
        with:
          java-version: '25'
          distribution: 'temurin'
          cache: maven

      - name: Install dependencies for acknowledgments script
        run: |
          sudo apt-get update                                                                                                                                                                                                                                                         
          sudo apt-get install -y jq curl                                                                                                                                                                                                                                             

      - name: Generate acknowledgments data
        run: |
          chmod +x scripts/generate-acknowledgments.sh                                                                                                                                                                                                                                
          ./scripts/generate-acknowledgments.sh
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build JAR without tests
        run: mvn compile package -DskipTests

      - name: Create bundle
        run: mkdir staging && cp target/*.jar staging

      - name: Upload JAR artifact
        uses: actions/upload-artifact@v4
        with:
          name: jar-artifact
          path: staging/
          retention-days: 1

  unit-tests:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up JDK 25
        uses: actions/setup-java@v4
        with:
          java-version: '25'
          distribution: 'temurin'
          cache: maven

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Run unit tests
        run: mvn test
        env:
          DOCKER_HOST: unix:///var/run/docker.sock
          SPRING_PROFILES_ACTIVE: test,ci

      - name: Upload coverage reports as artifact
        uses: actions/upload-artifact@v4
        with:
          name: coverage-reports
          path: |
            target/site/jacoco/
          retention-days: 30

  e2e-tests:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download JAR artifact
        uses: actions/download-artifact@v4
        with:
          name: jar-artifact
          path: staging/

      - name: Copy JAR to target directory for Docker build
        run: |
          mkdir -p target
          cp staging/*.jar target/

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Cache Docker layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: docker-${{ runner.os }}-${{ github.sha }}
          restore-keys: |
            docker-${{ runner.os }}-                                                                                                                                                                                                                                                  

      - name: Build docker image
        uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64
          context: .
          push: false
          load: true
          tags: dedicatedcode/reitti:latest
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

      - name: Move Docker cache
        run: |
          rm -rf /tmp/.buildx-cache                                                                                                                                                                                                                                                   
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache                                                                                                                                                                                                                                

      - name: Start docker-compose
        run: docker compose -f docker-compose.ci.yml up -d
        working-directory: e2e

      - name: Wait for app to be ready
        run: |
          timeout 60 bash -c 'until curl -f http://localhost:8080; do sleep 2; done'
        working-directory: e2e

      - name: Cache npm dependencies
        uses: actions/cache@v4
        with:
          path: e2e/node_modules
          key: npm-${{ runner.os }}-${{ hashFiles('e2e/package-lock.json') }}
          restore-keys: |
            npm-${{ runner.os }}-                                                                                                                                                                                                                                                     

      - name: Install dependencies
        run: npm ci
        working-directory: e2e

      - name: Cache Playwright browsers
        uses: actions/cache@v4
        with:
          path: ~/.cache/ms-playwright
          key: playwright-${{ runner.os }}-${{ hashFiles('e2e/package-lock.json') }}
          restore-keys: |
            playwright-${{ runner.os }}-                                                                                                                                                                                                                                              

      - name: Install Playwright browsers
        run: npx playwright install --with-deps
        working-directory: e2e

      - name: Run Playwright tests
        run: CI=1 npx playwright test --project=chromium --project=firefox --project=webkit
        env:
          BASE_URL: http://localhost:8080
        working-directory: e2e

      - name: Upload Playwright test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-test-results
          path: e2e/test-results/
          retention-days: 30

      - name: Upload Playwright report
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report
          path: e2e/playwright-report/
          retention-days: 30

      - name: Stop docker-compose
        run: docker compose -f docker-compose.ci.yml down
        working-directory: e2e
        if: always()
