name: Build
on:
  push:
    branches: [main, master]
    paths:
      - "landing/**"
      - ".gitea/workflows/build.yaml"

env:
  REGISTRY: git.realmanual.ru
  IMAGE_PREFIX: ${{ gitea.repository }}

permissions:
  contents: read
  packages: write

jobs:
  # Landing → static nginx image pushed to the Gitea registry.
  # (The macOS .dmg is built locally via `make dmg` — Tauri can't cross-compile
  # a macOS bundle on a Linux runner, and there is no self-hosted macOS runner.)
  landing:
    name: Build & push landing
    runs-on: ubuntu-22.04
    container: catthehacker/ubuntu:act-latest
    outputs:
      version: ${{ steps.version.outputs.VERSION }}
    steps:
      - uses: actions/checkout@v3
      - name: Read version
        id: version
        run: echo "VERSION=$(cat ./landing/VERSION)" >> $GITHUB_OUTPUT
      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: ./landing
          file: ./landing/Dockerfile
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/spacesh-landing:${{ steps.version.outputs.VERSION }}
            ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/spacesh-landing:latest

  notify:
    name: Notify Max
    needs: landing
    if: always()
    runs-on: ubuntu-22.04
    container: catthehacker/ubuntu:act-latest
    steps:
      - name: Compose & send summary
        run: |
          case "${{ needs.landing.result }}" in
            success) line="✅ spacesh-landing собран (\`${{ needs.landing.outputs.version }}\`)";;
            failure) line="❌ spacesh-landing — ошибка сборки";;
            *)       line="❔ spacesh-landing — ${{ needs.landing.result }}";;
          esac
          url="${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_number }}"
          text=$(printf '**Build landing**\n\n%s\n\n[лог](%s)' "$line" "$url")
          curl -s -X POST "https://platform-api.max.ru/messages?chat_id=${{ secrets.MAX_CHAT_ID }}" \
            -H "Authorization: ${{ secrets.MAX_BOT_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d "$(jq -n --arg t "$text" '{text:$t,format:"markdown"}')"
