name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version (leave empty to use current)'
        required: false
      update_version:
        description: 'Update version and commit version bump?'
        required: true
        default: true
        type: boolean

jobs:
  setup:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    env:
      RELEASE_VERSION: ${{ github.event.inputs.version }}
    outputs:
      release_version: ${{ steps.get_version.outputs.release_version }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Get version from build.gradle.kts if not provided
        id: get_version
        run: |
          if [ -z "${{ github.event.inputs.version }}" ]; then
            VERSION=$(grep '^version = ' build.gradle.kts | sed 's/version = "\(.*\)"/\1/')
            echo "release_version=$VERSION" >> $GITHUB_OUTPUT
            echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
          else
            echo "release_version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
            echo "RELEASE_VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
          fi

      - name: Update version in build.gradle.kts
        if: ${{ github.event.inputs.update_version }}
        run: |
          sed -i "s/^version = .*/version = \"$RELEASE_VERSION\"/" build.gradle.kts

      - name: Update version in app/package.json
        if: ${{ github.event.inputs.update_version }}
        run: |
          jq ".version = \"$RELEASE_VERSION\"" app/package.json > app/package.json.tmp && mv app/package.json.tmp app/package.json

      - name: Upload modified files
        uses: actions/upload-artifact@v6
        with:
          name: modified-files
          path: |
            build.gradle.kts
            app/package.json

  build:
    needs: setup
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write
      checks: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download modified files
        uses: actions/download-artifact@v7
        with:
          name: modified-files

      - name: Set up JDK 21
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: '21'
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5

      - name: Run production build
        env:
          GAMEYFIN_KEYSTORE_PASSWORD: ${{ secrets.GAMEYFIN_KEYSTORE_PASSWORD }}
        run: ./gradlew clean build -Pvaadin.productionMode=true

      - name: Publish Test Report
        uses: mikepenz/action-junit-report@v6
        if: success() || failure() # always run even if the previous step fails
        with:
          report_paths: '**/build/test-results/test/TEST-*.xml'

      - name: Upload build outputs
        uses: actions/upload-artifact@v6
        with:
          name: build-outputs
          path: |
            app/build/libs/**
            plugins/**/build/libs/**/*.jar

  docker:
    needs: [ setup, build ]
    runs-on: ubuntu-latest
    permissions:
      packages: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download modified files
        uses: actions/download-artifact@v7
        with:
          name: modified-files

      - name: Download build outputs
        uses: actions/download-artifact@v7
        with:
          name: build-outputs
          path: .

      - name: Generate container image tags
        id: docker_tags
        run: |
          VERSION='${{ needs.setup.outputs.release_version }}'
          GHCR_TAGS="ghcr.io/gameyfin/gameyfin:$VERSION"
          if [[ "$VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
            MAJOR=${BASH_REMATCH[1]}
            MINOR=${BASH_REMATCH[2]}
            PATCH=${BASH_REMATCH[3]}
            GHCR_TAGS="ghcr.io/gameyfin/gameyfin:latest,ghcr.io/gameyfin/gameyfin:develop,ghcr.io/gameyfin/gameyfin:$VERSION,ghcr.io/gameyfin/gameyfin:$MAJOR.$MINOR,ghcr.io/gameyfin/gameyfin:$MAJOR"
          fi
          TAGS="$GHCR_TAGS"
          echo "tags=$TAGS" >> $GITHUB_OUTPUT

      - name: Build and push Docker image
        uses: ./.github/actions/docker-build-push
        with:
          ghcr_username: ${{ github.actor }}
          ghcr_token: ${{ secrets.GITHUB_TOKEN }}
          context: .
          dockerfile: docker/Dockerfile
          platforms: linux/arm64/v8,linux/amd64
          tags: ${{ steps.docker_tags.outputs.tags }}

  plugin_api:
    needs: setup
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download modified files
        uses: actions/download-artifact@v7
        with:
          name: modified-files

      - name: Set up JDK 21
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5

      - name: Build and push Plugin-API
        run: ./gradlew publishAndReleaseToMavenCentral --no-configuration-cache
        env:
          ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_PRIVATE_KEY }}
          ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.GPG_PASSPHRASE }}
          ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVENCENTRAL_USERNAME }}
          ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVENCENTRAL_PASSWORD }}

  finalize:
    needs: [ docker, plugin_api ]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Download modified files
        uses: actions/download-artifact@v7
        with:
          name: modified-files

      - name: Commit version bump
        if: ${{ github.event.inputs.update_version }}
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: 'chore: release v${{ github.event.inputs.version }}'
          tagging_message: v${{ github.event.inputs.version }}

      - name: Detect prerelease
        id: detect_prerelease
        run: |
          if [[ "${{ github.event.inputs.version }}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo "IS_PRERELEASE=false" >> $GITHUB_ENV
            echo "MAKE_LATEST=true" >> $GITHUB_ENV
          else
            echo "IS_PRERELEASE=true" >> $GITHUB_ENV
            echo "MAKE_LATEST=false" >> $GITHUB_ENV
          fi

      - name: Create GitHub release
        if: ${{ github.event.inputs.update_version }}
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ github.event.inputs.version }}
          prerelease: ${{ env.IS_PRERELEASE }}
          make_latest: ${{ env.MAKE_LATEST }}
