name: Build and Push Docker Image (fix/*)

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

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write
      checks: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

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

      - 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: build
    runs-on: ubuntu-latest
    permissions:
      packages: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

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

      - name: Extract tag from branch name
        id: extract_tag
        run: |
          BRANCH_NAME="${GITHUB_REF#refs/heads/}"
          TAG="${BRANCH_NAME#fix/}"
          echo "tag=$TAG" >> $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: ghcr.io/gameyfin/gameyfin:${{ steps.extract_tag.outputs.tag }}
