name: Build and Test

on:
  push:
    branches:
      - main
      - develop
  pull_request:
    branches:
      - main
      - develop
  workflow_dispatch:

concurrency:
  group: game-ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  GODOT_VERSION: "4.3"
  PROJECT_PATH: "./game"

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

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

      - name: Restore import cache
        uses: actions/cache@v4
        with:
          path: ${{ env.PROJECT_PATH }}/.godot/imported
          key: godot-import-${{ runner.os }}-${{ hashFiles('game/**/*.import', 'game/project.godot') }}
          restore-keys: |
            godot-import-${{ runner.os }}-

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

      - name: Validate project
        run: godot --headless --path "$PROJECT_PATH" --editor --quit

      - name: Run unit tests
        run: |
          godot --headless \
            --path "$PROJECT_PATH" \
            -s res://addons/gut/gut_cmdln.gd \
            -gdir=res://tests \
            -gexit

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

    strategy:
      fail-fast: false
      matrix:
        include:
          - target: linux
            preset: Linux
            output: builds/linux/starlight-valley.x86_64
          - target: windows
            preset: Windows
            output: builds/windows/starlight-valley.exe
          - target: web
            preset: Web
            output: builds/web/index.html

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

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

      - name: Create output directory
        run: mkdir -p "$(dirname "$OUTPUT_PATH")"
        env:
          OUTPUT_PATH: ${{ matrix.output }}

      - name: Export game
        run: |
          godot --headless \
            --path "$PROJECT_PATH" \
            --export-release "$EXPORT_PRESET" \
            "$OUTPUT_PATH"
        env:
          EXPORT_PRESET: ${{ matrix.preset }}
          OUTPUT_PATH: ${{ matrix.output }}

      - name: Package build
        run: |
          archive="starlight-valley-${TARGET}-${GITHUB_SHA::7}.zip"
          cd "$(dirname "$OUTPUT_PATH")"
          zip -r "../../../$archive" .
        env:
          TARGET: ${{ matrix.target }}
          OUTPUT_PATH: ${{ matrix.output }}

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: starlight-valley-${{ matrix.target }}-${{ github.sha }}
          path: starlight-valley-${{ matrix.target }}-*.zip
          if-no-files-found: error
          retention-days: 14

  release:
    name: Publish Release Artifacts
    if: startsWith(github.ref, 'refs/tags/v')
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          path: release
          merge-multiple: true

      - name: Publish GitHub release
        uses: softprops/action-gh-release@v2
        with:
          files: release/*.zip
          generate_release_notes: true
          prerelease: ${{ contains(github.ref_name, '-beta') || contains(github.ref_name, '-rc') }}