# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Release Cordova

on:
  push:
    branches: [ release ]

jobs:
  release:
    runs-on: ubuntu-latest
    environment: release

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 22.x
        cache: "yarn"
    - name: 🧶 Yarn install
      run: yarn ci

    - name: 🔍Lint code
      run: yarn lint
      continue-on-error: true

    - name: 📚Read package.json
      id: pkg
      uses: jaywcjlove/github-action-package@v1.3.0

    - name: Set Version Number Variable
      id: versionNumber
      uses: actions/github-script@v6
      with:
        result-encoding: string
        script: |
          // TODO✏ automate this
          var run_number = ${{ github.run_number }} - 151
          var version = '${{ steps.pkg.outputs.version }}'
          var version_number = version + '.' + run_number
          return version_number
    - name: Set App ID Variable
      id: appId
      uses: actions/github-script@v6
      with:
        result-encoding: string
        script: |
          return '${{ steps.pkg.outputs.name }}'

    - name: ⬆ Update package.json version
      uses: jossef/action-set-json-field@v2.1
      with:
        file: package.json
        field: version
        value: ${{ steps.versionNumber.outputs.result }}

    - name: ⬆ Update package.json app environment
      uses: jossef/action-set-json-field@v2.1
      with:
        file: package.json
        field: name
        value: ${{ steps.appId.outputs.result }}

    - name: ⬆ Update package.json product name
      uses: jossef/action-set-json-field@v2.1
      with:
        file: package.json
        field: productName
        value: ${{ steps.pkg.outputs.productName }}

    - name: 📦 Pack for 🕸web with Node.js
      run: yarn pack:web

    - name: 🐋 Build docker image
      run: |
        docker build -t ghcr.io/marmadilemanteater/freetubecordova:${{ steps.versionNumber.outputs.result }} .
        docker build -t ghcr.io/marmadilemanteater/freetubecordova:latest .

    - name: 🗝 Login to Github Container Registry
      run: |
        echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u MarmadileManteater --password-stdin

    - name: 📡 Push docker image to Github Container Registry
      run: |
        docker push ghcr.io/marmadilemanteater/freetubecordova:${{ steps.versionNumber.outputs.result }}
        docker push ghcr.io/marmadilemanteater/freetubecordova:latest

    - name: 📡 Upload PWA Artifact
      uses: actions/upload-artifact@v4.6.0
      with:
        name: freetube-${{ steps.versionNumber.outputs.result }}-PWA
        path: dist/web

    - name: 🔨 Setup Zip Action
      uses: montudor/action-zip@v1.0.0

    - name: 🤐 Zip output
      run: zip -qq -r freetube-${{ steps.versionNumber.outputs.result }}.zip *
      working-directory: dist/web/

    - name: 📦 Pack for 📱Android with Node.js & Cordova
      run: |
        yarn pack:botGuardScript:android
        yarn pack:android

    - name: 🦴 Fetch keystore from secrets
      run: |
         while read -r line;
         do
           echo "${line}" >> freetube.keystore.asc
         done <<< '${{ secrets.KEYSTORE }}'
         gpg -d --passphrase '${{ secrets.KEYSTORE_PASSWORD }}' --batch freetube.keystore.asc >> freetube.keystore

    - name: Inject signing config from secrets
      run: |
        sed -i 's@// inject signing config@// inject signing config\r\nstorePassword = "${{ secrets.KEYSTORE_PASSWORD }}"@' android/app/build.gradle.kts
        sed -i 's@// inject signing config@// inject signing config\r\nstoreFile = file("../../freetube.keystore")@' android/app/build.gradle.kts
        sed -i 's@// inject signing config@// inject signing config\r\nkeyPassword = "${{ secrets.KEYSTORE_PASSWORD }}"@' android/app/build.gradle.kts
        sed -i 's@// inject signing config@keyAlias = "freetubecordova"@' android/app/build.gradle.kts
        cat android/app/build.gradle.kts

    - name: Update name
      run: |
        sed -i 's/"FreeTube Android"/"FreeTube"/g' android/settings.gradle.kts
        sed -i 's/FreeTube Android/FreeTube/g' android/app/src/main/res/values/strings.xml

    - name: set up JDK 17
      uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: gradle

    - name: Grant execute permission for gradlew
      run: cd android/ && chmod +x gradlew

    - name: Build with Gradle
      run: cd android/ && ./gradlew assembleRelease

    - name: Rename APK w/ version info
      run: |
        cp android/app/build/outputs/apk/release/app-release.apk dist/freetube-${{ steps.versionNumber.outputs.result }}-Android.apk

    - name: 📡 Upload APK Artifact
      uses: actions/upload-artifact@v4.6.0
      with:
        name: freetube-${{ steps.versionNumber.outputs.result }}-Android.apk
        path: dist/freetube-${{ steps.versionNumber.outputs.result }}-Android.apk

    - name: Create release body
      run: |
        echo "${{ github.event.head_commit.message }}" >> release.txt
        echo "" >> release.txt

    - name: 📝 Create Draft Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.versionNumber.outputs.result }}
        release_name: ${{ steps.versionNumber.outputs.result }}
        draft: true
        prerelease: false
        body_path: release.txt

    - name: ⬆ Upload HTML5 Artifact to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: dist/web/freetube-${{ steps.versionNumber.outputs.result }}.zip
        asset_name: freetube-${{ steps.versionNumber.outputs.result }}-pwa.zip
        asset_content_type: application/zip

    - name: ⬆ Upload Android APK Artifact to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: dist/freetube-${{ steps.versionNumber.outputs.result }}-Android.apk
        asset_name: freetube-${{ steps.versionNumber.outputs.result }}-Android.apk
        asset_content_type: application/apk
