name: Build and Upload Release (Windows EXE)

on:
  release:
    types: [published]
  workflow_dispatch: {}

jobs:
  build-windows-exe:
    name: Build Windows EXE
    runs-on: windows
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Create .env file from secrets
        shell: powershell
        run: |
          $envContent = @"
          # Default Collection URLs
          CORE_COLLECTION_URL=${{ secrets.CORE_COLLECTION_URL || 'https://steamcommunity.com/workshop/filedetails/?id=3521297585' }}
          CONTENT_COLLECTION_URL=${{ secrets.CONTENT_COLLECTION_URL || 'steam://openurl/https://steamcommunity.com/sharedfiles/filedetails/?id=3521319712' }}
          COSMETICS_COLLECTION_URL=${{ secrets.COSMETICS_COLLECTION_URL || 'steam://openurl/https://steamcommunity.com/sharedfiles/filedetails/?id=3637541646' }}

          # ModsConfig.xml Path Template
          MODSCONFIG_PATH_TEMPLATE=${{ secrets.MODSCONFIG_PATH_TEMPLATE || '%USERPROFILE%\AppData\LocalLow\Ludeon Studios\RimWorld by Ludeon Studios\Config\ModsConfig.xml' }}
          "@
          $envContent | Out-File -FilePath .env -Encoding UTF8 -Force
          Write-Host "Created .env file with configuration"

      - name: Install dependencies
        shell: powershell
        run: |
          python -m pip install --upgrade pip
          if (Test-Path requirements.txt) { pip install -r requirements.txt }
          pip install pyinstaller

      - name: Build EXE with PyInstaller
        shell: powershell
        run: |
          $ErrorActionPreference = 'Stop'
          # Build standalone executable with all assets bundled
          pyinstaller --clean --onefile --windowed --icon=art/Progression.ico --add-data "art;art" --add-data ".env;." --name ProgressionLoader steam_workshop_gui.py

      - name: Prepare artifact
        shell: powershell
        run: |
          # Just upload the standalone .exe file
          Copy-Item dist\ProgressionLoader.exe ProgressionLoader.exe

      - name: Upload asset to Release
        if: ${{ github.event_name == 'release' && github.event.action == 'published' }}
        shell: powershell
        env:
          TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO: ${{ github.repository }}
          RELEASE_ID: ${{ github.event.release.id }}
          SERVER_URL: ${{ github.server_url }}
        run: |
          $ErrorActionPreference = 'Stop'
          # Gitea API endpoint for uploading release assets
          $uploadUrl = "$env:SERVER_URL/api/v1/repos/$env:REPO/releases/$env:RELEASE_ID/assets"
          Write-Host "Uploading ProgressionLoader.exe to $uploadUrl"
          
          # Use multipart form data for Gitea
          $boundary = [System.Guid]::NewGuid().ToString()
          $LF = "`r`n"
          
          $fileBytes = [System.IO.File]::ReadAllBytes("ProgressionLoader.exe")
          $fileEnc = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($fileBytes)
          
          $bodyLines = (
              "--$boundary",
              "Content-Disposition: form-data; name=`"attachment`"; filename=`"ProgressionLoader.exe`"",
              "Content-Type: application/octet-stream$LF",
              $fileEnc,
              "--$boundary--$LF"
          ) -join $LF
          
          Invoke-RestMethod -Method Post -Uri $uploadUrl -Headers @{ 
              Authorization = "token $env:TOKEN"
              "Content-Type" = "multipart/form-data; boundary=$boundary"
          } -Body $bodyLines

