name: Starling Grove CI

on:
  pull_request:
    branches: [main, develop]
  push:
    branches: [main, develop]
    tags: ["v*"]
  workflow_dispatch:
    inputs:
      build_target:
        description: Platform to build
        required: true
        default: StandaloneWindows64
        type: choice
        options:
          - StandaloneWindows64
          - StandaloneOSX
          - StandaloneLinux64

env:
  UNITY_VERSION: "2022.3.58f1"
  PROJECT_PATH: "."
  GAME_NAME: "Moonlit Courier"

jobs:
  test:
    name: EditMode and PlayMode Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout project
        uses: actions/checkout@v4
        with:
          lfs: true

      - name: Restore Unity cache
        uses: actions/cache@v4
        with:
          path: Library
          key: Library-${{ env.UNITY_VERSION }}-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}
          restore-keys: |
            Library-${{ env.UNITY_VERSION }}-

      - name: Run Unity tests
        uses: game-ci/unity-test-runner@v4
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          projectPath: ${{ env.PROJECT_PATH }}
          unityVersion: ${{ env.UNITY_VERSION }}
          testMode: all
          artifactsPath: test-results

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: unity-test-results
          path: test-results

  build:
    name: Build ${{ matrix.targetPlatform }}
    runs-on: ubuntu-latest
    needs: test
    strategy:
      fail-fast: false
      matrix:
        targetPlatform:
          - StandaloneWindows64
          - StandaloneLinux64
          - StandaloneOSX

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

      - name: Restore Unity cache
        uses: actions/cache@v4
        with:
          path: Library
          key: Library-${{ env.UNITY_VERSION }}-${{ matrix.targetPlatform }}-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}
          restore-keys: |
            Library-${{ env.UNITY_VERSION }}-${{ matrix.targetPlatform }}-

      - name: Build game
        uses: game-ci/unity-builder@v4
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          projectPath: ${{ env.PROJECT_PATH }}
          unityVersion: ${{ env.UNITY_VERSION }}
          targetPlatform: ${{ matrix.targetPlatform }}
          buildName: ${{ env.GAME_NAME }}
          buildsPath: Builds

      - name: Package build
        run: |
          mkdir -p release
          cd Builds/${{ matrix.targetPlatform }}
          zip -r "../../release/${{ env.GAME_NAME }}-${{ matrix.targetPlatform }}-${{ github.sha }}.zip" .

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.GAME_NAME }}-${{ matrix.targetPlatform }}
          path: release/*.zip
          retention-days: 14

  publish:
    name: Publish Tagged Build
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Download builds
        uses: actions/download-artifact@v4
        with:
          path: release

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          files: release/**/*.zip
          draft: true
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}