name: Desktop App
on:
  push:
    branches:
      - main
    tags:
      - 'desktop-*'
      - 'v[0-9]+.[0-9]+.[0-9]*'

jobs:
  build:
    name: Build Linux (amd64)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22'

      - run: npm ci
        working-directory: web

      - run: npm run build
        working-directory: web

      - run: |
          rm -rf desktop/frontend/dist
          mkdir -p desktop/frontend/dist
          cp -r web/dist/* desktop/frontend/dist/

      - uses: actions/setup-go@v5
        with:
          go-version: '1.26'

      - run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev

      - run: CGO_ENABLED=1 go build -o build/bin/Oikos .
        working-directory: desktop

      - run: |
          cd desktop/build/bin
          tar czf oikos-desktop-linux-amd64.tar.gz Oikos
          sha256sum oikos-desktop-linux-amd64.tar.gz > oikos-desktop-linux-amd64.tar.gz.sha256

      - uses: actions/upload-artifact@v4
        with:
          name: oikos-desktop-linux-amd64
          path: |
            desktop/build/bin/oikos-desktop-linux-amd64.tar.gz
            desktop/build/bin/oikos-desktop-linux-amd64.tar.gz.sha256

  release:
    name: Attach to Release
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: oikos-desktop-linux-amd64

      - uses: https://gitea.com/actions/release-action@v1
        with:
          files: |
            oikos-desktop-linux-amd64.tar.gz
            oikos-desktop-linux-amd64.tar.gz.sha256
          api_key: ${{ secrets.GITEA_TOKEN }}
