name: Build and Release

on: push

jobs:
  build:
    name: Build on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-22.04
            platform: linux
            python-version: "3.11"
          - os: windows-latest
            platform: windows
            python-version: "3.11"

    steps:
      - name: Check-out repository
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
          architecture: "x64"
          cache: "pip"
          cache-dependency-path: |
            **/requirements*.txt

      - name: Install Dependencies
        run: pip install -r requirements.txt

      - name: Build Executable
        uses: Nuitka/Nuitka-Action@main
        with:
          nuitka-version: main
          script-name: run.py
          nofollow-import-to: "*tk*,_codecs,encodings,multiprocessing,gi"
          disable-plugins: tk-inter,dill-compat,eventlet,gevent,pyqt5,pyqt6,pyside2,pyside6,delvewheel,pywebview,matplotlib,spacy,enum-compat,pbr-compat,gevent,pmw-freezer,transformers,upx,kivy,options-nanny,multiprocessing,gi
          include-data-dir: assets=assets
          include-data-files: CREDITS=CREDITS
          mode: onefile
          output-file: csd4ni3lBrowser

      - name: Locate and rename executable (Linux)
        if: matrix.os == 'ubuntu-22.04'
        run: |
          set -euo pipefail
          echo "Searching for built Linux binary..."
          # List to help debugging when paths change
          ls -laR . | head -n 500 || true
          BIN=$(find . -maxdepth 4 -type f -name 'csd4ni3lBrowser*' -perm -u+x | head -n1 || true)
          if [ -z "${BIN}" ]; then
            echo "ERROR: No Linux binary found after build"
            exit 1
          fi
          echo "Found: ${BIN}"
          mkdir -p build_output
          cp "${BIN}" build_output/csd4ni3lBrowser.bin
          chmod +x build_output/csd4ni3lBrowser.bin
          echo "Executable ready: build_output/csd4ni3lBrowser.bin"
        shell: bash

      - name: Locate and rename executable (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          Write-Host "Searching for built Windows binary..."
          Get-ChildItem -Recurse -File -Filter 'csd4ni3lBrowser*.exe' | Select-Object -First 1 | ForEach-Object {
            Write-Host ("Found: " + $_.FullName)
            New-Item -ItemType Directory -Force -Path build_output | Out-Null
            Copy-Item $_.FullName "build_output\csd4ni3lBrowser.exe"
            Write-Host "Executable ready: build_output\csd4ni3lBrowser.exe"
          }
          if (!(Test-Path build_output\csd4ni3lBrowser.exe)) {
            Write-Error "ERROR: No Windows binary found after build"
            exit 1
          }
        shell: pwsh

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.platform }}
          path: build_output/csd4ni3lBrowser.*

  release:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download All Build Artifacts
        uses: actions/download-artifact@v4
        with:
          path: downloads

      - name: Create release (if missing) and upload artifacts to tag
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -euo pipefail
          TAG="latest"
          echo "Target release tag: $TAG"
          if gh release view "$TAG" >/dev/null 2>&1; then
            echo "Release $TAG already exists; will upload assets with --clobber"
          else
            gh release create "$TAG" \
              --title "$TAG" \
              --notes "Automated build for $TAG"
          fi
          # Upload the executables directly (no zip files)
          gh release upload "$TAG" downloads/linux/csd4ni3lBrowser.bin --clobber
          gh release upload "$TAG" downloads/windows/csd4ni3lBrowser.exe --clobber
