name: CI
permissions:
  contents: read
  checks: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

on:
  workflow_dispatch:
  workflow_call:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint-test:
    name: Lint, Static Analysis & Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          lfs: true

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v6
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/master' }}
          cache-cleanup: on-success

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Run Lint, Static Analysis & JVM Tests with Coverage
        run: ./gradlew lintDebug ktlintCheck detekt verifyWithCoverage -Proborazzi.test.verify=true -Pcoverage

      - name: Upload HTML Coverage Report
        uses: actions/upload-artifact@v7
        with:
          name: jacoco-html-report
          path: app/build/reports/jacoco/verifyWithCoverage/html/
          retention-days: 7

      - name: Upload Roborazzi Results
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: roborazzi-results
          path: app/build/test-results/roborazzi
          retention-days: 7

  build:
    name: Build & Verify Release
    runs-on: ubuntu-latest
    needs: [ lint-test ]
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

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

      - name: Set up Gradle
        uses: gradle/actions/setup-gradle@v6
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/master' }}
          cache-cleanup: on-success

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build Debug APK
        run: ./gradlew assembleDebug

      - name: Build Release APK & Minify with R8
        run: ./gradlew assembleRelease

      - name: Verify R8 Stripping (DebugBillingOverride)
        run: |
          MAPPING_FILE="app/build/outputs/mapping/release/mapping.txt"
          if [ ! -f "$MAPPING_FILE" ]; then
            echo "Error: R8 mapping file not found at $MAPPING_FILE"
            exit 1
          fi
          # Match only top-level class entries (e.g. "com.yorvana.data.billing.DebugBillingOverride -> x:"
          # or nested "...DebugBillingOverride$Inner -> ..."), not method-signature references
          # in other classes that R8 retains for stack-trace deobfuscation, and not the
          # DebugBillingOverrideMode enum (which is intentionally retained in release).
          if grep -qE '^com\.yorvana\.data\.billing\.DebugBillingOverride( |\$)' "$MAPPING_FILE"; then
            echo "Error: DebugBillingOverride class found in release mapping file. R8 stripping failed!"
            grep -E '^com\.yorvana\.data\.billing\.DebugBillingOverride( |\$)' "$MAPPING_FILE"
            exit 1
          fi
          echo "Success: DebugBillingOverride class is absent from release build."

      - name: Upload Debug APK
        uses: actions/upload-artifact@v7
        with:
          name: debug-apk
          path: app/build/outputs/apk/debug/*.apk
          if-no-files-found: error
          retention-days: 1

  ci-success:
    name: CI Success
    if: ${{ always() }}
    runs-on: ubuntu-latest
    needs: [ lint-test, build ]
    steps:
      - name: Verify required jobs succeeded
        run: |
          if [ "${{ needs.lint-test.result }}" != "success" ] || \
             [ "${{ needs.build.result }}" != "success" ]; then
            echo "One or more required jobs did not succeed:"
            echo "lint-test=${{ needs.lint-test.result }}"
            echo "build=${{ needs.build.result }}"
            exit 1
          fi

          echo "All checks passed successfully!"
