name: DSPRE Beta Canary Build

on:
  push:
    branches:
      - main  # Trigger only on pushes to the main branch

permissions:
  contents: write
  actions: write

env:
  SOLUTION_FILE_PATH: DS_Map.sln
  BUILD_CONFIGURATION: Release

jobs:
  build:
    if: github.repository == 'Mixone-FInallyHere/DS-Pokemon-Rom-Editor'
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2

    - name: Add msbuild to PATH
      uses: microsoft/setup-msbuild@v2

    - name: Clone Database Repository
      run: |
        git clone https://github.com/DS-Pokemon-Rom-Editor/scrcmd-database.git databases

    - name: Restore NuGet packages
      working-directory: ${{env.GITHUB_WORKSPACE}}
      run: nuget restore ${{env.SOLUTION_FILE_PATH}}

    - name: Build DSPRE
      working-directory: ${{env.GITHUB_WORKSPACE}}
      run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
    
    - name: Prepare Release Files
      run: |
        mkdir "${{env.GITHUB_WORKSPACE}}DS_Map\bin\Release\databases"
        robocopy databases "${{env.GITHUB_WORKSPACE}}DS_Map\bin\Release\databases" /E /XD "databases\.git\objects\pack"
        if ($LASTEXITCODE -le 7) { exit 0 } else { exit 1 }

    - name: Zip Release Files
      run: Compress-Archive -Path ${{env.GITHUB_WORKSPACE}}DS_Map\bin\Release -DestinationPath DSPRE-beta-canary.zip

    - name: Fetch tags
      run: git fetch --prune --unshallow --tags

    - name: Update canary tag
      run: |
       git config user.name "github-actions[bot]"
       git config user.email "github-actions[bot]@users.noreply.github.com"
       git tag -f beta_canary
       git push -f origin beta_canary

    - name: Delete previous release
      run: |
        gh release delete --yes beta_canary

      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        
    - name: Get current date
      id: date
      run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
        
    - name: Create new release
      uses: softprops/action-gh-release@da05d552573ad5aba039eaac05058a918a7bf631
      with:
        tag_name: beta_canary
        name: DSPRE Beta Canary Build ${{ steps.date.outputs.date }}
        files: DSPRE-beta-canary.zip
        token: ${{ secrets.GITHUB_TOKEN }}
        body: |
          ##  ⚠️ Canary Build Notice ⚠️
          This is a Canary Build of DSPRE Beta, meaning it is automatically generated from the latest pushed code. It includes the most recent changes, improvements, and experimental features, but may be unstable or contain bugs.
          
          ## 🚧 What does this mean?
          This build reflects ongoing development and is not a stable release.
          Features may be incomplete, subject to change, or cause unexpected issues.
          Use at your own risk—expect potential crashes, broken functionality, or unpolished features.
          
          ## 💡 Who is this for?
          Developers and testers who want to stay up-to-date with the latest progress.
          Users willing to report issues and provide feedback on new changes.
          If you’re looking for a more reliable version, consider using the latest Stable Release instead.
        prerelease: true
        generate_release_notes: true
