name: Scheduled Updates (Firmware, Hardware, Translations)

on:
  schedule:
    - cron: '0 * * * *' # Run every hour
  workflow_dispatch:     # Allow manual triggering

jobs:
  update_and_translate:
    runs-on: ubuntu-latest
    if: github.repository == 'meshtastic/Meshtastic-Android'
    permissions:
      contents: write      # To commit files and push branches
      pull-requests: write # To create pull requests

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Update firmware releases list
        run: |
          firmware_file_path="app/src/main/assets/firmware_releases.json"
          temp_firmware_file="/tmp/new_firmware_releases.json"
          
          echo "Fetching latest firmware releases..."
          curl -s --fail https://api.meshtastic.org/github/firmware/list > "$temp_firmware_file"
          
          if ! jq empty "$temp_firmware_file" 2>/dev/null; then
            echo "::error::Firmware API returned invalid JSON data. Skipping firmware update."
          else
            if [ ! -f "$firmware_file_path" ] || ! jq --sort-keys . "$temp_firmware_file" | diff -q - <(jq --sort-keys . "$firmware_file_path"); then
              echo "Changes detected in firmware list or local file missing. Updating $firmware_file_path."
              cp "$temp_firmware_file" "$firmware_file_path"
            else
              echo "No changes detected in firmware list."
            fi
          fi

      - name: Update hardware list
        run: |
          hardware_file_path="app/src/main/assets/device_hardware.json"
          temp_hardware_file="/tmp/new_device_hardware.json"

          echo "Fetching latest device hardware data..."
          curl -s --fail https://api.meshtastic.org/resource/deviceHardware > "$temp_hardware_file"

          if ! jq empty "$temp_hardware_file" 2>/dev/null; then
            echo "::error::Hardware API returned invalid JSON data. Skipping hardware update."
          else
            if [ ! -f "$hardware_file_path" ] || ! jq --sort-keys . "$temp_hardware_file" | diff -q - <(jq --sort-keys . "$hardware_file_path"); then
              echo "Changes detected in hardware list or local file missing. Updating $hardware_file_path."
              cp "$temp_hardware_file" "$hardware_file_path"
            else
              echo "No changes detected in hardware list."
            fi
          fi

      - name: Upload sources to Crowdin
        uses: crowdin/github-action@v2
        with:
          base_url: 'https://meshtastic.crowdin.com/api/v2'
          config: 'config/crowdin/crowdin.yml'
          crowdin_branch_name: 'main'
          upload_sources: true
          upload_translations: false
          download_translations: false
          create_pull_request: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

      - name: Download translations from Crowdin
        uses: crowdin/github-action@v2
        with:
          base_url: 'https://meshtastic.crowdin.com/api/v2'
          config: 'config/crowdin/crowdin.yml'
          crowdin_branch_name: 'main'
          upload_sources: false
          download_translations: true
          create_pull_request: false
          commit_message: 'chore(l10n): New Crowdin Translations from scheduled update'
          push_translations: false
          push_sources: false
          localization_branch_name: ${{ github.ref_name }}
          # skip_ref_checkout: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

      - name: Create Pull Request if changes occurred
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: |
            chore: Scheduled updates (Firmware, Hardware, Translations)

            Automated updates for:
            - Firmware releases list
            - Device hardware list
            - Crowdin source string uploads
            - Crowdin translation downloads
          title: 'chore: Scheduled updates (Firmware, Hardware, Translations)'
          body: |
            This PR includes automated updates from the scheduled workflow:

            - Updated `firmware_releases.json` from the Meshtastic API (if changed).
            - Updated `device_hardware.json` from the Meshtastic API (if changed).
            - Source strings were uploaded to Crowdin (if any local changes were pushed).
            - Latest translations were downloaded from Crowdin (if available).

            Please review the changes.
          branch: 'scheduled-updates'
          base: 'main'
          delete-branch: true
          labels: |
            automation
            l10n
            firmware
            hardware