name: Build and Publish Astrolabe App Release

on:
  push:
    tags:
      - 'astrolabe-v*'

env:
  APP_NAME: astrolabe
  APP_DIR: third_party/astrolabe

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get version from tag
        id: tag
        run: |
          echo "TAG=${GITHUB_REF#refs/tags/astrolabe-v}" >> $GITHUB_OUTPUT

      - name: Validate version in info.xml matches tag
        working-directory: ${{ env.APP_DIR }}
        run: |
          INFO_VERSION=$(sed -n 's/.*<version>\(.*\)<\/version>.*/\1/p' appinfo/info.xml | tr -d '\t')
          if [ "$INFO_VERSION" != "${{ steps.tag.outputs.TAG }}" ]; then
            echo "Version mismatch: info.xml has $INFO_VERSION but tag is ${{ steps.tag.outputs.TAG }}"
            exit 1
          fi
          echo "Version validated: $INFO_VERSION"

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.1
          coverage: none

      - name: Checkout Nextcloud server (for signing)
        uses: actions/checkout@v4
        with:
          repository: nextcloud/server
          ref: stable30
          path: server

      - name: Install dependencies and build
        working-directory: ${{ env.APP_DIR }}
        run: |
          composer install --no-dev --optimize-autoloader
          npm ci
          npm run build

      - name: Setup signing certificate
        run: |
          mkdir -p $HOME/.nextcloud/certificates
          echo "${{ secrets.APP_PRIVATE_KEY }}" > $HOME/.nextcloud/certificates/${{ env.APP_NAME }}.key
          echo "${{ secrets.APP_PUBLIC_CRT }}" > $HOME/.nextcloud/certificates/${{ env.APP_NAME }}.crt

      - name: Build app store package
        working-directory: ${{ env.APP_DIR }}
        run: make appstore server_dir=${{ github.workspace }}/server

      - name: Create GitHub release and attach tarball
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ env.APP_DIR }}/build/artifacts/${{ env.APP_NAME }}.tar.gz
          asset_name: ${{ env.APP_NAME }}-${{ steps.tag.outputs.TAG }}.tar.gz
          tag: ${{ github.ref }}
          release_name: Astrolabe ${{ steps.tag.outputs.TAG }}
          prerelease: ${{ contains(steps.tag.outputs.TAG, '-alpha') || contains(steps.tag.outputs.TAG, '-beta') || contains(steps.tag.outputs.TAG, '-rc') }}

      - name: Upload to Nextcloud App Store
        uses: R0Wi/nextcloud-appstore-push-action@v1.0.6
        with:
          app_name: ${{ env.APP_NAME }}
          appstore_token: ${{ secrets.APPSTORE_TOKEN }}
          download_url: ${{ github.server_url }}/${{ github.repository }}/releases/download/${{ github.ref_name }}/${{ env.APP_NAME }}-${{ steps.tag.outputs.TAG }}.tar.gz
          app_private_key: ${{ secrets.APP_PRIVATE_KEY }}
          nightly: ${{ contains(steps.tag.outputs.TAG, '-alpha') || contains(steps.tag.outputs.TAG, '-beta') || contains(steps.tag.outputs.TAG, '-rc') }}
