name: Compile and package harmony_composer
on:
  push:
    branches:
      - master

jobs:
  package_harmony_composer:
    container:
      image: hub.nationtech.io/harmony/harmony_composer:latest
    runs-on: dind
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Build for Linux x86_64
        run: cargo build --release --bin harmony_composer --target x86_64-unknown-linux-gnu

      - name: Build for Windows x86_64 GNU
        run: cargo build --release --bin harmony_composer --target x86_64-pc-windows-gnu

      - name: Setup log into hub.nationtech.io
        uses: docker/login-action@v3
        with:
          registry: hub.nationtech.io
          username: ${{ secrets.HUB_BOT_USER }}
          password: ${{ secrets.HUB_BOT_PASSWORD }}

      # TODO: build ARM images and MacOS binaries (or other targets) too

      - name: Update snapshot-latest tag
        run: |
          git config user.name "Gitea CI"
          git config user.email "ci@nationtech.io"
          git tag -f snapshot-latest
          git push origin snapshot-latest --force

      - name: Install jq
        run: apt install -y jq # The current image includes apt lists so we don't have to apt update and rm /var/lib/apt... every time. But if the image is optimized it won't work anymore

      - name: Create or update release
        run: |
          # First, check if release exists and delete it if it does
          RELEASE_ID=$(curl -s -X GET \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            "https://git.nationtech.io/api/v1/repos/nationtech/harmony/releases/tags/snapshot-latest" \
            | jq -r '.id // empty')

          if [ -n "$RELEASE_ID" ]; then
            # Delete existing release
            curl -X DELETE \
              -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
              "https://git.nationtech.io/api/v1/repos/nationtech/harmony/releases/$RELEASE_ID"
          fi

          # Create new release
          RESPONSE=$(curl -X POST \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -H "Content-Type: application/json" \
            -d '{
              "tag_name": "snapshot-latest",
              "name": "Latest Snapshot",
              "body": "Automated snapshot build from master branch",
              "draft": false,
              "prerelease": true
            }' \
            "https://git.nationtech.io/api/v1/repos/nationtech/harmony/releases")

          echo "RELEASE_ID=$(echo $RESPONSE | jq -r '.id')" >> $GITHUB_ENV

      - name: Upload Linux binary
        run: |
          curl -X POST \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary "@target/x86_64-unknown-linux-gnu/release/harmony_composer" \
            "https://git.nationtech.io/api/v1/repos/nationtech/harmony/releases/${{ env.RELEASE_ID }}/assets?name=harmony_composer"

      - name: Upload Windows binary
        run: |
          curl -X POST \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary "@target/x86_64-pc-windows-gnu/release/harmony_composer.exe" \
            "https://git.nationtech.io/api/v1/repos/nationtech/harmony/releases/${{ env.RELEASE_ID }}/assets?name=harmony_composer.exe"

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: hub.nationtech.io/harmony/harmony_composer:latest
