name: Starling Cove CI

on:
  pull_request:
    branches:
      - main
      - release/*
  push:
    branches:
      - main
    tags:
      - "v*.*.*"

env:
  UNITY_VERSION: "2022.3.58f1"
  PROJECT_PATH: "."
  BUILD_NAME: "StarlingCove"
  STEAM_APP_ID: "1847200"

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

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

      - name: Check required project files
        run: |
          test -f ProjectSettings/ProjectVersion.txt
          test -d Assets
          test -d Packages

      - name: Verify Git LFS assets
        run: git lfs fsck

  test:
    name: Unity Tests
    needs: validate
    runs-on: ubuntu-latest
    timeout-minutes: 60

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

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

      - name: Run edit mode and play 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:
          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 }}
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 90

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

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

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

      - 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

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

  deploy-steam-beta:
    name: Deploy Steam Beta
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    timeout-minutes: 45

    steps:
      - name: Download Windows build
        uses: actions/download-artifact@v4
        with:
          name: StarlingCove-StandaloneWindows64
          path: steam-build/windows

      - name: Download Linux build
        uses: actions/download-artifact@v4
        with:
          name: StarlingCove-StandaloneLinux64
          path: steam-build/linux

      - name: Download macOS build
        uses: actions/download-artifact@v4
        with:
          name: StarlingCove-StandaloneOSX
          path: steam-build/macos

      - name: Publish to Steam beta branch
        uses: game-ci/steam-deploy@v3
        with:
          username: ${{ secrets.STEAM_USERNAME }}
          password: ${{ secrets.STEAM_PASSWORD }}
          configVdf: ${{ secrets.STEAM_CONFIG_VDF }}
          ssfnFileName: ${{ secrets.STEAM_SSFN_FILE_NAME }}
          ssfnFileContents: ${{ secrets.STEAM_SSFN_FILE_CONTENTS }}
          appId: ${{ env.STEAM_APP_ID }}
          buildDescription: ${{ github.ref_name }}
          rootPath: steam-build
          depot1Path: windows
          depot2Path: linux
          depot3Path: macos
          releaseBranch: beta