name: Nightly Build

on:
  schedule:
    - cron: ${{ vars.CRON_SCHEDULE }}
  workflow_dispatch:

env:
  PROJECT_NAME: ${{ vars.PROJECT_NAME }}
  BUILD_CONFIG: ${{ vars.BUILD_CONFIGURATION }}
  RETENTION_DAYS: ${{ vars.ARTIFACT_RETENTION_DAYS }}

jobs:
  check-changes:
    runs-on: ubuntu-latest
    outputs:
      changed: ${{ steps.check.outputs.changed }}
      commit_count: ${{ steps.check.outputs.commit_count }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Check for changes in last 24h
        id: check
        run: |
          CHANGES=$(git log --since="24 hours ago" --oneline | wc -l)
          if [ "$CHANGES" -gt 0 ]; then
            echo "changed=true" >> $GITHUB_OUTPUT
            echo "commit_count=$CHANGES" >> $GITHUB_OUTPUT
          else
            echo "changed=false" >> $GITHUB_OUTPUT
            echo "commit_count=0" >> $GITHUB_OUTPUT
          fi

  build-linux:
    needs: check-changes
    if: needs.check-changes.outputs.changed == 'true'
    runs-on: ${{ vars.LINUX_RUNNER_LABEL }}
    timeout-minutes: 120
    env:
      ENGINE_DIR: /opt/unreal-engine
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true

      - name: Pull LFS files
        run: git lfs pull

      - name: Verify engine path
        run: |
          if [ ! -d "$ENGINE_DIR" ]; then
            echo "::error::Engine not found at $ENGINE_DIR"
            exit 1
          fi
          echo "Engine found: $ENGINE_DIR"

      - name: Build Linux ${{ env.BUILD_CONFIG }}
        run: |
          SHORT_SHA="${{ github.sha }}"
          ARCHIVE_NAME="${PROJECT_NAME}-Linux-${SHORT_SHA::7}.tar.gz"

          "$ENGINE_DIR/Engine/Build/BatchFiles/Linux/Build.sh" \
            "${PROJECT_NAME}" Linux "${BUILD_CONFIG}" \
            -Project="$(pwd)/${PROJECT_NAME}.uproject" \
            -NoP4 -UTF8Output -Clean

          tar -czf "$ARCHIVE_NAME" \
            -C Binaries/Linux \
            "${PROJECT_NAME}-Linux-${BUILD_CONFIG}" \
            ../Content/Paks/* 2>/dev/null || true

          echo "ARCHIVE_NAME=$ARCHIVE_NAME" >> $GITHUB_ENV

      - name: Upload Linux artifact
        uses: actions/upload-artifact@v4
        with:
          name: linux-build
          path: ${{ env.ARCHIVE_NAME }}
          retention-days: ${{ env.RETENTION_DAYS }}

  release:
    needs: [build-linux]
    if: >-
      always() &&
      needs.build-linux.result == 'success'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Generate version info
        id: version
        run: |
          DATE=$(date +'%Y%m%d')
          SHA_SHORT="${{ github.sha }}"
          TAG="nightly-${DATE}-${SHA_SHORT::7}"
          echo "tag=$TAG" >> $GITHUB_OUTPUT
          echo "date=$DATE" >> $GITHUB_OUTPUT
          echo "sha_short=${SHA_SHORT::7}" >> $GITHUB_OUTPUT

      - name: List artifacts
        run: ls -lah *.tar.gz *.zip 2>/dev/null || echo "No artifacts found"

      - name: Create Release & Upload Assets
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.tag }}
          name: "🌙 Nightly ${{ steps.version.outputs.date }} (${{ steps.version.outputs.sha_short }})"
          body: |
            ## Nightly Build
            
            | Property | Value |
            |----------|-------|
            | Commit | `${{ github.sha }}` |
            | Date | ${{ steps.version.outputs.date }} |
            | Config | ${{ vars.BUILD_CONFIGURATION }} |
            | Changes | ${{ needs.check-changes.outputs.commit_count }} commits |
            
            ### Downloads
            - 🐧 **Linux:** `${{ vars.PROJECT_NAME }}-Linux-${{ steps.version.outputs.sha_short }}.tar.gz`
            ---
            *⚠️ Auto-generated nightly build. May be unstable.*
          draft: false
          prerelease: true
          files: |
            *.tar.gz
            *.zip
        env:
          GITHUB_TOKEN: ${{ secrets.UNREAL_TOKEN }}