name: Game Build Pipeline

on:
  push:
    branches: [main, develop]
    tags: ["v*"]
  pull_request:
    branches: [main, develop]
  workflow_dispatch:

permissions:
  contents: read

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

env:
  ENGINE_VERSION: "4.3.2"
  PROJECT_PATH: "./StarfallTavern"
  BUILD_PATH: "./build"

jobs:
  validate:
    name: Validate Project
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          lfs: true

      - name: Verify asset pointers
        run: git lfs fsck

      - name: Check project metadata
        run: |
          test -f "$PROJECT_PATH/project.godot"
          test -d "$PROJECT_PATH/assets"
          test -d "$PROJECT_PATH/scripts"

  test:
    name: Automated Tests
    needs: validate
    runs-on: ubuntu-latest
    timeout-minutes: 20
    container:
      image: barichello/godot-ci:4.3.2
    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          lfs: true

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

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

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

  build:
    name: Build ${{ matrix.target }}
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: windows
            preset: Windows Desktop
            artifact: starfall-tavern-windows
            output: StarfallTavern.exe
          - target: linux
            preset: Linux/X11
            artifact: starfall-tavern-linux
            output: StarfallTavern.x86_64
          - target: web
            preset: Web
            artifact: starfall-tavern-web
            output: index.html
    container:
      image: barichello/godot-ci:4.3.2
    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          lfs: true

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

      - name: Prepare output directory
        run: mkdir -p "$BUILD_PATH/${{ matrix.target }}"

      - name: Export game
        run: |
          godot --headless \
            --path "$PROJECT_PATH" \
            --export-release "${{ matrix.preset }}" \
            "../build/${{ matrix.target }}/${{ matrix.output }}"

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

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

      - name: Package builds
        run: |
          mkdir -p packages
          for directory in artifacts/*; do
            archive="packages/$(basename "$directory").zip"
            cd "$directory"
            zip -r "../../$archive" .
            cd ../..
          done

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