name: Build and Release

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: default
    steps:
      - name: Install dependencies
        run: |
          nix-env -iA nixpkgs.nodejs nixpkgs.openssh nixpkgs.curl nixpkgs.jq nixpkgs.dotnetCorePackages.sdk_10_0-bin
          dotnet --version

      - name: Checkout
        uses: actions/checkout@v4

      - name: Fetch APK from private repo
        run: |
          curl -fsSL \
            -H "Authorization: token ${{ secrets.APK_REPO_TOKEN }}" \
            "https://git.amarth.cloud/api/v1/repos/chris/files/raw/delver.apk" \
            -o delver.apk

      - name: Build all platforms
        run: |
          export PATH="$HOME/.dotnet:$PATH"
          # Windows
          dotnet publish -c Release -r win-x64 -p:PublishReadyToRun=false -o dist/win-x64/standard
          dotnet publish -c Release -r win-x64 -p:PublishReadyToRun=false -p:EmbeddedApk=delver.apk -o dist/win-x64/embedded
          # Linux
          dotnet publish -c Release -r linux-x64 -p:PublishReadyToRun=false -o dist/linux-x64/standard
          dotnet publish -c Release -r linux-x64 -p:PublishReadyToRun=false -p:EmbeddedApk=delver.apk -o dist/linux-x64/embedded
          # macOS
          dotnet publish -c Release -r osx-x64 -p:PublishReadyToRun=false -o dist/osx-x64/standard
          dotnet publish -c Release -r osx-x64 -p:PublishReadyToRun=false -p:EmbeddedApk=delver.apk -o dist/osx-x64/embedded

      - name: Package binaries
        run: |
          mkdir -p release
          # Standard builds
          cp dist/win-x64/standard/Scry.exe release/scry-win-x64.exe
          cp dist/linux-x64/standard/Scry release/scry-linux-x64
          cp dist/osx-x64/standard/Scry release/scry-osx-x64
          # Embedded builds
          cp dist/win-x64/embedded/Scry.exe release/scry-embedded-win-x64.exe
          cp dist/linux-x64/embedded/Scry release/scry-embedded-linux-x64
          cp dist/osx-x64/embedded/Scry release/scry-embedded-osx-x64
          # Make Linux/macOS binaries executable
          chmod +x release/scry-linux-x64 release/scry-osx-x64
          chmod +x release/scry-embedded-linux-x64 release/scry-embedded-osx-x64

      - name: Create Release
        env:
          FORGEJO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG="build-${{ github.run_number }}"
          REPO="${{ github.repository }}"
          API_URL="https://git.amarth.cloud/api/v1"
          
          # Create release
          RELEASE_ID=$(curl -fsSL -X POST \
            -H "Authorization: token $FORGEJO_TOKEN" \
            -H "Content-Type: application/json" \
            -d "{\"tag_name\": \"$TAG\", \"name\": \"$TAG\", \"body\": \"Automated release from CI\"}" \
            "$API_URL/repos/$REPO/releases" | jq -r '.id')
          
          # Upload assets
          for file in release/*; do
            filename=$(basename "$file")
            curl -fsSL -X POST \
              -H "Authorization: token $FORGEJO_TOKEN" \
              -H "Content-Type: application/octet-stream" \
              --data-binary "@$file" \
              "$API_URL/repos/$REPO/releases/$RELEASE_ID/assets?name=$filename"
          done
