name: "ci"

on:
  push:
    branches: [develop, staging, main]
  pull_request:
    branches: [develop, staging, main]

permissions:
  contents: write
  pull-requests: write

jobs:
  lint-commit:
    runs-on: "ubuntu-latest"
    steps:
      - uses: "actions/checkout@v4.2.2"
      - uses: "wagoid/commitlint-github-action@v6.2.1"

  lint-editorconfig-checker:
    runs-on: "ubuntu-latest"
    steps:
      - uses: "actions/checkout@v4.2.2"
      - uses: "editorconfig-checker/action-editorconfig-checker@main"
      - run: "editorconfig-checker"

  build-windows:
    runs-on: "ubuntu-latest"
    outputs:
      build-path: ${{ steps.upload.outputs.path }}
    steps:
      - uses: "actions/checkout@v4.2.2"
        with:
          lfs: true

      - uses: "actions/cache@v4.2.3"
        with:
          path: "Library"
          key: "Library-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}"
          restore-keys: |
            "Library-"

      - uses: "game-ci/unity-builder@v4.3.0"
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          targetPlatform: "StandaloneWindows64"

      - name: Upload Windows Build Artifact
        id: upload
        if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v4.4.3
        with:
          name: build-windows
          path: build/

  build-macos:
    runs-on: "ubuntu-latest"
    outputs:
      build-path: ${{ steps.upload.outputs.path }}
    steps:
      - uses: "actions/checkout@v4.2.2"
        with:
          lfs: true

      - uses: "actions/cache@v4.2.3"
        with:
          path: "Library"
          key: "Library-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}"
          restore-keys: |
            "Library-"

      - uses: "game-ci/unity-builder@v4.3.0"
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          targetPlatform: "StandaloneOSX"

      - name: Upload macOS Build Artifact
        id: upload
        if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v4.3.0
        with:
          name: build-macos
          path: build/

  build-linux:
    runs-on: "ubuntu-latest"
    outputs:
      build-path: ${{ steps.upload.outputs.path }}
    steps:
      - uses: "actions/checkout@v4.2.2"
        with:
          lfs: true

      - uses: "actions/cache@v4.2.3"
        with:
          path: "Library"
          key: "Library-${{ hashFiles('Assets/**', 'Packages/**', 'ProjectSettings/**') }}"
          restore-keys: |
            "Library-"

      - uses: "game-ci/unity-builder@v4.3.0"
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          targetPlatform: "StandaloneLinux64"

      - name: Upload Linux Build Artifact
        id: upload
        if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v4.6.2
        with:
          name: build-linux
          path: build/

  create-release:
    needs: [build-windows, build-macos, build-linux]
    if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Get version from commit message
        id: get_version
        run: |
          COMMIT_MESSAGE="${{ github.event.head_commit.message }}"
          # Extract the version number after "chore(release): "
          VERSION=$(echo "$COMMIT_MESSAGE" | sed -n 's/^chore(release): \(v[0-9]*\.[0-9]*\.[0-9]*[-a-zA-Z0-9.]*\).*$/\1/p')
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Download Windows Build Artifact
        uses: actions/download-artifact@v4.3.0
        with:
          name: build-windows
          path: build/windows

      - name: Download macOS Build Artifact
        uses: actions/download-artifact@v4.3.0
        with:
          name: build-macos
          path: build/macos

      - name: Download Linux Build Artifact
        uses: actions/download-artifact@v4.3.0
        with:
          name: build-linux
          path: build/linux

      - name: Zip Builds
        run: |
          cd build/windows
          zip -r ../windows.zip .
          cd ../macos
          zip -r ../macos.zip .
          cd ../linux
          zip -r ../linux.zip .

      - name: Create GitHub Release
        id: create_release
        uses: softprops/action-gh-release@v2.2.2
        with:
          tag_name: ${{ steps.get_version.outputs.version }}
          name: ${{ steps.get_version.outputs.version }}
          body: |
            🎮 Automatic ${{ github.ref == 'refs/heads/main' && 'Production' || 'Pre-release' }} build
          prerelease: ${{ github.ref != 'refs/heads/main' }}
          files: |
            build/windows.zip
            build/macos.zip
            build/linux.zip
