name: Build and Release

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: default
    container:
      image: mcr.microsoft.com/dotnet/sdk:9.0
    steps:
      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y git ssh nodejs
      - name: Checkout
        uses: actions/checkout@v4

      - name: Fetch APK from private repo
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.APK_DEPLOY_KEY }}" > ~/.ssh/apk_key
          chmod 600 ~/.ssh/apk_key
          GIT_SSH_COMMAND="ssh -i ~/.ssh/apk_key -o StrictHostKeyChecking=no" \
            git clone --depth 1 git@git.amarth.cloud:chris/.files.git apk-repo
          mv apk-repo/delver.apk .
          rm -rf apk-repo ~/.ssh/apk_key

      - name: Build all platforms
        run: |
          # Windows
          dotnet publish -c Release -r win-x64 -o dist/win-x64/standard
          dotnet publish -c Release -r win-x64 -p:EmbeddedApk=delver.apk -o dist/win-x64/embedded
          # Linux
          dotnet publish -c Release -r linux-x64 -o dist/linux-x64/standard
          dotnet publish -c Release -r linux-x64 -p:EmbeddedApk=delver.apk -o dist/linux-x64/embedded
          # macOS
          dotnet publish -c Release -r osx-x64 -o dist/osx-x64/standard
          dotnet publish -c Release -r osx-x64 -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
        uses: https://code.forgejo.org/actions/forgejo-release@v2
        with:
          direction: upload
          release-dir: release
          release-notes: "Automated release from CI"
          tag: ${{ github.run_number }}
