name: Build and Release Starling Harbor

on:
  push:
    branches: [main]
    tags: ["v*.*.*"]
  pull_request:
    branches: [main]
  workflow_dispatch:

env:
  GODOT_VERSION: "4.3"
  EXPORT_PRESET: "release"
  PROJECT_PATH: "."
  ARTIFACT_RETENTION_DAYS: 14

jobs:
  build:
    name: Export ${{ matrix.target }}
    runs-on: ubuntu-22.04

    strategy:
      fail-fast: false
      matrix:
        include:
          - target: windows
            preset: "Windows Desktop"
            output: "build/windows/StarlingHarbor.exe"
            artifact: "starling-harbor-windows"
          - target: linux
            preset: "Linux"
            output: "build/linux/StarlingHarbor.x86_64"
            artifact: "starling-harbor-linux"
          - target: web
            preset: "Web"
            output: "build/web/index.html"
            artifact: "starling-harbor-web"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          lfs: true

      - name: Restore Godot import cache
        uses: actions/cache@v4
        with:
          path: |
            .godot/imported
            .godot/editor
          key: godot-${{ env.GODOT_VERSION }}-${{ matrix.target }}-${{ hashFiles('**/*.import', 'project.godot') }}

      - name: Setup Godot
        uses: chickensoft-games/setup-godot@v2
        with:
          version: ${{ env.GODOT_VERSION }}
          use-dotnet: false
          include-templates: true

      - name: Verify export presets
        run: godot --headless --path "${PROJECT_PATH}" --export-pack "${{ matrix.preset }}" /tmp/preset-check.pck

      - name: Export game
        run: |
          mkdir -p "$(dirname "${{ matrix.output }}")"
          godot --headless --path "${PROJECT_PATH}" --export-release "${{ matrix.preset }}" "${{ matrix.output }}"

      - name: Package build
        run: |
          cd build/${{ matrix.target }}
          zip -r "../../${{ matrix.artifact }}.zip" .

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}.zip
          retention-days: ${{ env.ARTIFACT_RETENTION_DAYS }}

  publish-itch:
    name: Publish tagged build to itch.io
    runs-on: ubuntu-22.04
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist

      - name: Install butler
        run: |
          curl -L -o butler.zip https://broth.itch.ovh/butler/linux-amd64/LATEST/archive/default
          unzip butler.zip
          chmod +x butler

      - name: Push Windows build
        run: ./butler push dist/starling-harbor-windows/starling-harbor-windows.zip tiny-aurora/starling-harbor:windows --userversion "${GITHUB_REF_NAME}"
        env:
          BUTLER_API_KEY: ${{ secrets.ITCH_IO_API_KEY }}

      - name: Push Linux build
        run: ./butler push dist/starling-harbor-linux/starling-harbor-linux.zip tiny-aurora/starling-harbor:linux --userversion "${GITHUB_REF_NAME}"
        env:
          BUTLER_API_KEY: ${{ secrets.ITCH_IO_API_KEY }}

      - name: Push Web build
        run: ./butler push dist/starling-harbor-web/starling-harbor-web.zip tiny-aurora/starling-harbor:web --userversion "${GITHUB_REF_NAME}"
        env:
          BUTLER_API_KEY: ${{ secrets.ITCH_IO_API_KEY }}