#
# Copyright (C) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

# Grab downloads

name: Downloads Count

on:
  schedule:
    - cron: '0 0 * * *'

jobs:

  get-all-releases-count:
    runs-on: ubuntu-24.04

    steps:
      - uses: actions/github-script@v7
        id: process-downloads
        with:
          script: |
            const resp = await github.paginate('GET /repos/{owner}/{repo}/releases{?per_page,page}', {
                owner: 'containers',
                repo: 'podman-desktop',
                per_page: 100
            }, (response) => response.data.map(({ tag_name, assets }) => ({ tag_name, assets })));

            const releases = resp;

            // Keep only tag_name and asset.name and asset.download_count from releases
            releases.forEach((release) => { release.assets = release.assets.map(({ name, download_count }) => ({ name, download_count })) })

            let totalMac = 0;
            let totalWin = 0;
            let totalLinux = 0;

            // count the total number of downloads for each release for mac,windows,linux assests
            releases.forEach((release) => { 
                release.assets = release.assets.filter((asset) => { 
                    if (asset.name.endsWith(".dmg") || (asset.name.endsWith("arm64.zip") || asset.name.endsWith("x64.zip") || asset.name.endsWith("universal.zip")) ){
                        totalMac += asset.download_count;
                    } else if (asset.name.endsWith(".exe")){
                        totalWin += asset.download_count;
                    } else if (asset.name.endsWith(".tar.gz") || asset.name.endsWith(".flatpak")){
                        totalLinux+=asset.download_count;
                    }
                })
            })

            let totalDownloads = totalMac + totalWin + totalLinux;

            // Export results as environment variables
            core.exportVariable('totalDownloads', totalDownloads);
            core.exportVariable('totalMac', totalMac);
            core.exportVariable('totalWin', totalWin);
            core.exportVariable('totalLinux', totalLinux);
        
      - name: send-event-4
        run: |
            curl https://api.segment.io/v1/track \
            -X POST \
            -H 'Content-Type: application/json' \
            -d '{
              "userId": "gh-action",
              "event": "download-count",
               "properties": {
                "mac": "${{ env.totalMac }}",
                "windows": "${{ env.totalWin }}",
                "linux": "${{ env.totalLinux }}",
                "total": "${{ env.totalDownloads }}"
              }
            }' \
            -u ${{ secrets.SEGMENT_WRITE_KEY }}
