name: build-and-test

"on":
  push:
    branches: [main, release/*]
  pull_request:
    branches: [main]
  workflow_dispatch:
    inputs:
      target:
        description: Build target
        required: true
        default: StandaloneWindows64
        type: choice
        options:
          - StandaloneWindows64
          - StandaloneOSX
          - WebGL

env:
  UNITY_VERSION: 2022.3.47f1
  PROJECT_PATH: .
  BUILD_NAME: moonforge-prototype
  CACHE_VERSION: v3

jobs:
  test:
    name: Unity EditMode and PlayMode Tests
    runs-on: ubuntu-latest
    timeout-minutes: 45

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

      - name: Cache Unity Library
        uses: actions/cache@v4
        with:
          path: Library
          key: Library-${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ hashFiles('Packages/packages-lock.json', 'ProjectSettings/ProjectVersion.txt') }}
          restore-keys: |
            Library-${{ env.CACHE_VERSION }}-${{ runner.os }}-

      - 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
          retention-days: 14

  build:
    name: Build Game Client
    runs-on: ubuntu-latest
    needs: test
    timeout-minutes: 90

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

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

      - name: Cache Unity Library
        uses: actions/cache@v4
        with:
          path: Library
          key: Library-${{ env.CACHE_VERSION }}-${{ matrix.targetPlatform }}-${{ hashFiles('Packages/packages-lock.json', 'ProjectSettings/ProjectVersion.txt') }}
          restore-keys: |
            Library-${{ env.CACHE_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:
          projectPath: ${{ env.PROJECT_PATH }}
          unityVersion: ${{ env.UNITY_VERSION }}
          targetPlatform: ${{ matrix.targetPlatform }}
          buildName: ${{ env.BUILD_NAME }}
          buildsPath: Builds
          versioning: Semantic

      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BUILD_NAME }}-${{ matrix.targetPlatform }}
          path: Builds/${{ matrix.targetPlatform }}
          retention-days: 21

  deploy-webgl:
    name: Deploy WebGL Preview
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == 'refs/heads/main'

    steps:
      - name: Download WebGL artifact
        uses: actions/download-artifact@v4
        with:
          name: moonforge-prototype-WebGL
          path: public

      - name: Publish preview build
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: moonforge-preview
          directory: public
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}