name: Release Workflow

on:
  release:
    types:
      - created

concurrency:
  group: release_workflow
  cancel-in-progress: true

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build_web:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: cat .github/workflows/versions.env >> $GITHUB_ENV
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          cache: true
      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install nodejs -y
      - uses: moonrepo/setup-rust@v1
      - run: rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
      - run: flutter pub get
      - name: Prepare web
        run: ./scripts/prepare-web.sh
      - run: rm ./assets/vodozemac/.gitignore
      - name: Build Release Web
        run: flutter build web --dart-define=FLUTTER_WEB_CANVASKIT_URL=canvaskit/ --release --source-maps --base-href "/web/"
      - name: Create archive
        run: tar -czf fluffychat-web.tar.gz build/web/
      - name: Upload Web Build
        uses: actions/upload-artifact@v6
        with:
          name: Web Build
          path: fluffychat-web.tar.gz
      - name: Upload to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAGES_DEPLOY_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: fluffychat-web.tar.gz
          asset_name: fluffychat-web.tar.gz
          asset_content_type: application/gzip
      - name: Install and build tailwindcss
        working-directory: docs
        run: |
          npm install tailwindcss @tailwindcss/cli
          npx tailwindcss -o ./tailwind.css
      - name: Build Website
        run: |
          mv docs public
          mv repo public || true
          mv build/web/ public/web
          cp public/web -r public/nightly
      - name: Deploy to GitHub Pages
        if: startsWith(github.ref, 'refs/tags/v')
        uses: peaceiris/actions-gh-pages@v4
        with:
          personal_token: ${{ secrets.PAGES_DEPLOY_TOKEN }}
          publish_dir: ./public
          publish_branch: gh-pages
          cname: fluffychat.im

  build_apk:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: cat .github/workflows/versions.env >> $GITHUB_ENV
      - uses: actions/setup-java@v5
        with:
          java-version: ${{ env.JAVA_VERSION }}
          distribution: 'zulu'
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          cache: true
      - name: Add Firebase Messaging
        run: |
          echo '${{secrets.GOOGLE_SERVICES_JSON}}' | base64 -d > android/app/google-services.json
          ./scripts/add-firebase-messaging.sh
      - uses: ./.github/actions/free_up_space
      - uses: moonrepo/setup-rust@v1
      - run: flutter pub get
      - name: Prepare Android Release Build
        env:
          FDROID_KEY: ${{ secrets.FDROID_KEY }}
          FDROID_KEY_PASS: ${{ secrets.FDROID_KEY_PASS }}
          PLAYSTORE_DEPLOY_KEY: ${{ secrets.PLAYSTORE_DEPLOY_KEY }}
        run: ./scripts/prepare-android-release.sh
      - run: flutter build apk --release --target-platform android-arm,android-arm64
      - name: Upload to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAGES_DEPLOY_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: build/app/outputs/apk/release/app-release.apk
          asset_name: fluffychat.apk
          asset_content_type: application/vnd.android.package-archive

  build_linux:
    strategy:
      matrix:
        arch: [ x64, arm64 ]
    runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest'}}
    steps:
      - uses: actions/checkout@v6
      - run: cat .github/workflows/versions.env >> $GITHUB_ENV
      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install curl clang cmake ninja-build pkg-config libgtk-3-dev libblkid-dev liblzma-dev libjsoncpp-dev cmake-data libsecret-1-dev libsecret-1-0 librhash0 libssl-dev libwebkit2gtk-4.1-dev -y
      - name: Install Flutter
        run: |
          git clone --branch ${{ env.FLUTTER_VERSION }} https://github.com/flutter/flutter.git
          ./flutter/bin/flutter doctor
      - uses: moonrepo/setup-rust@v1
      - run: ./flutter/bin/flutter pub get
      - run: ./flutter/bin/flutter build linux --target-platform linux-${{ matrix.arch }}
      - name: Create archive
        run: tar -czf fluffychat-linux-${{ matrix.arch }}.tar.gz -C build/linux/${{ matrix.arch }}/release/bundle/ .
      - name: Upload to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAGES_DEPLOY_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: fluffychat-linux-${{ matrix.arch }}.tar.gz
          asset_name: fluffychat-linux-${{ matrix.arch }}.tar.gz
          asset_content_type: application/gzip

  deploy_playstore:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: cat .github/workflows/versions.env >> $GITHUB_ENV
      - uses: actions/setup-java@v5
        with:
          java-version: ${{ env.JAVA_VERSION }}
          distribution: 'zulu'
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          cache: true
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3'
      - uses: ./.github/actions/free_up_space
      - uses: moonrepo/setup-rust@v1
      - name: Install Fastlane
        working-directory: android
        run: gem install fastlane -NV
      - name: Add Firebase Messaging
        run: |
          echo '${{secrets.GOOGLE_SERVICES_JSON}}' | base64 -d > android/app/google-services.json
          ./scripts/add-firebase-messaging.sh
      - run: flutter pub get
      - name: Prepare Android Release Build
        env:
          FDROID_KEY: ${{ secrets.FDROID_KEY }}
          FDROID_KEY_PASS: ${{ secrets.FDROID_KEY_PASS }}
          PLAYSTORE_DEPLOY_KEY: ${{ secrets.PLAYSTORE_DEPLOY_KEY }}
        run: ./scripts/prepare-android-release.sh
      - name: Build Android Release
        run: flutter build appbundle --target-platform android-arm,android-arm64
      - name: Get Tag Name
        id: tag_name
        run: echo "::set-output name=tag::$(echo ${GITHUB_REF#refs/tags/})"
      - name: Deploy Android Release
        run: |
          mkdir -p build/android
          cp build/app/outputs/bundle/release/app-release.aab build/android/
          cd android
          bundle install
          bundle update fastlane
          bundle exec fastlane deploy_internal_test
          if [[ $GITHUB_REF_NAME == rc* ]]; then
              bundle exec fastlane deploy_candidate
          else
              bundle exec fastlane deploy_release
          fi
          cd ..

  deploy_docker:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Check out Git repository
        uses: actions/checkout@v6
      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
