name: build

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - platform: android
            os: ubuntu-latest
          - platform: windows
            os: windows-latest
          - platform: linux
            os: ubuntu-latest
          - platform: macos
            os: macos-13

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup JAVA
        if: startsWith(matrix.platform,'android')
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: 17

      - name: Setup NDK
        if: startsWith(matrix.platform,'android')
        uses: nttld/setup-ndk@v1
        id: setup-ndk
        with:
          ndk-version: r26b
          add-to-path: true
          link-to-sdk: true

      - name: Setup Android Signing
        if: startsWith(matrix.platform,'android')
        run: |
          echo "${{ secrets.KEYSTORE }}" | base64 --decode > android/app/keystore.jks
          echo "keyAlias=${{ secrets.KEY_ALIAS }}" >> android/local.properties    
          echo "storePassword=${{ secrets.STORE_PASSWORD }}" >> android/local.properties
          echo "keyPassword=${{ secrets.KEY_PASSWORD }}" >> android/local.properties
          

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version-file: 'core/go.mod'
          cache-dependency-path: |
            core/go.sum

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.x'
          channel: 'stable'
          cache: true

      - name: Get Flutter Dependency
        run: flutter pub get

      - name: Setup
        run: |
          dart setup.dart ${{ matrix.platform }}

      - name: Upload
        uses: actions/upload-artifact@v4
        with:
          name: artifact-${{ matrix.platform }}
          path: ./dist
          retention-days: 1
          overwrite: true


  upload-release:
    permissions: write-all
    needs: [ build ]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download
        uses: actions/download-artifact@v4
        with:
          path: ./dist/
          pattern: artifact-*
          merge-multiple: true

      - name: Pre Release
        run: |
          pip install gitchangelog pystache mustache markdown
          pre=$(curl --silent "https://api.github.com/repos/chen08209/FlClash/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' || echo "")
          if [ -z "pre" ]; then
            echo "init" > release.md
          else
            current="${{ github.ref_name }}"
            echo -e "\n\n<details markdown=1><summary>All changes from $current to the latest commit:</summary>\n\n" >> release.md
            gitchangelog "${pre}.." >> release.md 2>&1 || echo "Error in gitchangelog"
            echo -e "\n\n</details>" >> release.md
          fi
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: ./dist/*
          body_path: './release.md'