name: Release

on:
  push:
    branches:
      - 'release/**'

jobs:
  release:
    runs-on: docker
    
    steps:
      # Install software dependencies into the runner container
      - name: Install system dependencies
        run: |
          apt-get update
          apt-get install -y nodejs npm openjdk-17-jdk maven git
          export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
          echo "JAVA_HOME=$JAVA_HOME" >> $GITHUB_ENV

      - name: Check Java and Node versions
        run: |
          java -version
          mvn -v
          node -v
          npm -v
          
      # Checkout (clone) a fresh repo source of the project
      - name: Checkout repository
        uses: actions/checkout@v4
      
      # ##################################################################
      # ## THIS IS A RELEASE                                            ##
      # ##    #1. Get full version string                               ##
      # ##    #2. Remove sufix "-SNAPSHOT" from version string          ##
      # ##        And commit                                            ##
      # ##    #3. Tag the release                                       ##
      # ##                                                              ##
      - name: Extract version from pom.xml
        id: version
        run: |
          VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
          RELEASE_VERSION="${VERSION/-SNAPSHOT/}"
          echo "release_version=$RELEASE_VERSION" >> $GITHUB_OUTPUT

      - name: Set release version (remove -SNAPSHOT)
        run: |
          mvn versions:set -DnewVersion=${{ steps.version.outputs.release_version }}
          mvn versions:commit
          git config user.name "forgejo-actions"
          git config user.email "ci@forgejo.local"
          git commit -am "Release ${{ steps.version.outputs.release_version }}"
          git push

      - name: Tag release
        run: |
          git tag -a v${{ steps.version.outputs.release_version }} -m "Release ${{ steps.version.outputs.release_version }}"
          git push origin v${{ steps.version.outputs.release_version }}
      # ##                                                              ##
      # ##################################################################
            
      # Install @Angular dependencies
      - name: Install frontend dependencies
        run: |
          cd src/main/webapp
          npm ci

      # Setup node_modules cache, for better performance
      - name: Cache node_modules
        uses: actions/cache@v3
        with:
          path: |
            **/node_modules
          key: node-modules-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            node-modules-

      # Build the frontend
      - name: Build frontend
        run: |
          cd src/main/webapp
          npm run build

      # Setup Maven cache, for better performance
      - name: Cache Maven dependencies
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
        restore-keys: |
          maven-${{ runner.os }}-
      
      # Build the backend (JAR)
      - name: Build backend (Spring Boot)
        run: |
          mvn clean package -Pprod -DskipTests

      # Log output
      - name: List output files
        run: |
          ls -lh target/*.jar || true
          
      # save artifacts
      - name: Upload backend JAR
        uses: actions/upload-artifact@v3
        with:
          name: app-backend
          path: target/resilient*.jar
          
      # ##################################################################
      # ## THIS IS A RELEASE - Change master version to next SNAPSHOT   ##
      # ##    #4. Checkout master                                       ##
      # ##    #5. Calculate the new version string                      ##
      # ##        Update pom.xml                                        ##
      # ##        Calculate the new version string                      ##
      - name: Checkout main
        uses: actions/checkout@v3
        with:
          ref: master

      - name: Bump to next SNAPSHOT version
        run: |
          # Extract the version from the ref, handling tags like v1.0.2 or branches like release/1.0.2
          RAW_REF="${GITHUB_REF##*/}"  # gets 'v1.0.2' or '1.0.2'
          VERSION="${RAW_REF#v}"       # strips leading 'v' if present → gives '1.0.2'

          echo "Current version: $VERSION"

          MAJOR=$(echo "$VERSION" | cut -d. -f1)
          MINOR=$(echo "$VERSION" | cut -d. -f2)
          PATCH=$(echo "$VERSION" | cut -d. -f3)
          NEXT_PATCH=$((PATCH + 1))
          NEXT_VERSION="$MAJOR.$MINOR.$NEXT_PATCH-SNAPSHOT"

          echo "Next version: $NEXT_VERSION"

          mvn versions:set -DnewVersion="$NEXT_VERSION"
          mvn versions:commit

          git config user.name "forgejo-actions"
          git config user.email "ci@forgejo.local"
          git commit -am "Start next development cycle $NEXT_VERSION"
          git push origin master
      # ##                                                              ##
      # ##################################################################