name: build-and-test

on:
  push:
    branches:
      - main
      - develop
    paths:
      - "Assets/**"
      - "Packages/**"
      - "ProjectSettings/**"
      - ".github/workflows/build-and-test.yml"
  pull_request:
    branches:
      - main
      - develop

env:
  UNITY_VERSION: "2022.3.42f1"
  PROJECT_PATH: "."
  GAME_NAME: "MoonlightCourier"

jobs:
  validate:
    name: Validate Unity Project
    runs-on: ubuntu-latest
    timeout-minutes: 20

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

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

      - name: Run edit mode 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:
          unityVersion: ${{ env.UNITY_VERSION }}
          projectPath: ${{ env.PROJECT_PATH }}
          testMode: EditMode
          artifactsPath: test-results/editmode

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

  build:
    name: Build ${{ matrix.targetPlatform }}
    needs: validate
    runs-on: ubuntu-latest
    timeout-minutes: 60

    strategy:
      fail-fast: false
      matrix:
        targetPlatform:
          - StandaloneWindows64
          - StandaloneOSX
          - StandaloneLinux64

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

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

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

      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.GAME_NAME }}-${{ matrix.targetPlatform }}
          path: builds/${{ matrix.targetPlatform }}

  deploy-demo:
    name: Deploy Demo Build
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    timeout-minutes: 30

    steps:
      - name: Download Linux build
        uses: actions/download-artifact@v4
        with:
          name: MoonlightCourier-StandaloneLinux64
          path: release/linux

      - name: Publish to itch.io
        uses: manleydev/butler-publish-itchio-action@v1.0.3
        env:
          BUTLER_CREDENTIALS: ${{ secrets.BUTLER_API_KEY }}
          CHANNEL: linux-demo
          ITCH_GAME: moonlight-courier
          ITCH_USER: lanternfox-games
          PACKAGE: release/linux