name: Make Release
on:
  workflow_dispatch:
    inputs:
      prerelease:
        description: 'Mark as pre-release (e.g., for beta/RC releases)'
        type: boolean
        default: false

permissions:
  contents: read

concurrency:
  group: stable-release
  cancel-in-progress: true

jobs:
  prepare-release:
    permissions:
      contents: write  # Required for creating tags
    runs-on: ubuntu-latest
    outputs:
      release_tag: ${{ steps.release_commit.outputs.release_tag }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.UEPSEUDO_PAT }}

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'

      - name: Release Commit
        id: release_commit
        run: python release.py release_commit ${{ github.actor }}
        working-directory: ./tools/buildscripts

  build:
    needs: prepare-release
    uses: ./.github/workflows/cmake_build_ue4ss.yml
    secrets: inherit
    with:
      build-mode: "Game__Shipping__Win64"
      commit-sha: ${{ github.sha }}
      should-upload-artifact: true
      artifact-retention-days: 1
      enable-caching: false  # Disable caching for production releases
      build-ue4ss: true
      build-cppmods: true
      build-uvtd: false

  make-release:
    needs: [prepare-release, build]
    permissions:
      contents: write  # Required for creating releases
    runs-on: windows-2022
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
          token: ${{ secrets.UEPSEUDO_PAT }}

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'

      - name: Download Build Artifacts
        uses: actions/download-artifact@v4
        with:
          name: CMAKE-Game__Shipping__Win64
          path: build_output/

      - name: Verify downloaded files
        shell: pwsh
        run: |
          Write-Host "Contents of build_output:"
          Get-ChildItem -Path "build_output" -Recurse | ForEach-Object { Write-Host $_.FullName }

          $requiredFiles = @("UE4SS.dll", "dwmapi.dll")
          foreach ($file in $requiredFiles) {
            $filePath = Get-ChildItem -Path "build_output" -Recurse -Filter $file -ErrorAction SilentlyContinue | Select-Object -First 1
            if (-not $filePath) {
              Write-Error "$file not found in downloaded artifacts!"
              exit 1
            }
          }

      - name: Package
        id: package
        run: python tools/buildscripts/release.py package

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          tags: true

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ needs.prepare-release.outputs.release_tag }}
          prerelease: ${{ inputs.prerelease }}
          body_path: release/release_notes.md
          files: |
            release/UE4SS_v*.zip
            release/zDEV-UE4SS_v*.zip
            release/zCustomGameConfigs.zip
            release/zMapGenBP.zip
