name: Build VisionaryFX Electron App

on:
  workflow_dispatch:
    inputs:
      platform:
        description: 'Target platform'
        required: true
        default: 'windows'
        type: choice
        options:
          - windows
          - linux
          - both
  push:
    tags:
      - 'v*'

jobs:
  build-windows:
    if: ${{ github.event.inputs.platform == 'windows' || github.event.inputs.platform == 'both' || startsWith(github.ref, 'refs/tags/') }}
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

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

      - name: Install npm dependencies
        working-directory: electron-app
        run: npm install

      - name: Prepare bundled backend
        shell: bash
        run: |
          mkdir -p electron-app/bundled-backend/python
          mkdir -p electron-app/bundled-backend/data
          mkdir -p electron-app/bundled-backend/output
          mkdir -p electron-app/bundled-camoufox

          # Download Python embeddable
          curl -L -o python-embed.zip "https://www.python.org/ftp/python/3.12.7/python-3.12.7-embed-amd64.zip"
          unzip python-embed.zip -d electron-app/bundled-backend/python

          # Patch ._pth file
          for pth in electron-app/bundled-backend/python/*._pth; do
            sed -i 's/^#import site/import site/' "$pth"
            echo 'Lib\site-packages' >> "$pth"
            echo '..\.' >> "$pth"
          done

          # Download get-pip
          curl -sL -o electron-app/bundled-backend/python/get-pip.py https://bootstrap.pypa.io/get-pip.py

      - name: Install Python deps into embeddable
        shell: cmd
        run: |
          cd electron-app\bundled-backend
          python\python.exe python\get-pip.py --no-warn-script-location
          python\python.exe -m pip install -r ..\..\requirements.txt --no-warn-script-location
          python\python.exe -m pip install "camoufox[geoip]>=0.4.0" --no-warn-script-location

      - name: Copy project source
        shell: bash
        run: |
          for f in run_web.py config.py models.py pipeline.py presets.py output_formatter.py main.py cli.py camera_library.py requirements.txt; do
            [ -f "$f" ] && cp "$f" electron-app/bundled-backend/
          done
          for d in web agents providers character_templates scripts; do
            [ -d "$d" ] && cp -r "$d" electron-app/bundled-backend/
          done
          # Clean __pycache__
          find electron-app/bundled-backend -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true

      - name: Create setup script
        shell: bash
        run: |
          cat > electron-app/bundled-backend/setup-windows.bat << 'EOF'
          @echo off
          echo VisionaryFX first-run setup...
          cd /d "%~dp0"
          python\python.exe -c "from camoufox.pkgman import CamoufoxFetcher; f = CamoufoxFetcher(); f.download()" 2>nul
          del "%~dp0FIRST_RUN" 2>nul
          echo Setup complete!
          EOF
          echo "FIRST_RUN" > electron-app/bundled-backend/FIRST_RUN
          echo "PENDING" > electron-app/bundled-camoufox/FIRST_RUN

      - name: Build Electron (Windows)
        working-directory: electron-app
        run: npx electron-builder --win --x64
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: VisionaryFX-Windows
          path: |
            electron-app/dist/*.exe
            electron-app/dist/*.zip
          retention-days: 30

  build-linux:
    if: ${{ github.event.inputs.platform == 'linux' || github.event.inputs.platform == 'both' || startsWith(github.ref, 'refs/tags/') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

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

      - name: Install npm dependencies
        working-directory: electron-app
        run: npm install

      - name: Prepare bundled backend (Linux)
        run: |
          mkdir -p electron-app/bundled-backend electron-app/bundled-camoufox

          # Create venv
          python3 -m venv electron-app/bundled-backend/python
          source electron-app/bundled-backend/python/bin/activate
          pip install -r requirements.txt
          pip install "camoufox[geoip]>=0.4.0"

          # Copy source
          for f in run_web.py config.py models.py pipeline.py presets.py output_formatter.py main.py cli.py camera_library.py requirements.txt; do
            [ -f "$f" ] && cp "$f" electron-app/bundled-backend/
          done
          for d in web agents providers character_templates scripts; do
            [ -d "$d" ] && cp -r "$d" electron-app/bundled-backend/
          done
          find electron-app/bundled-backend -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true

          mkdir -p electron-app/bundled-backend/data electron-app/bundled-backend/output
          echo "PENDING" > electron-app/bundled-camoufox/FIRST_RUN

      - name: Build Electron (Linux)
        working-directory: electron-app
        run: npx electron-builder --linux --x64
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: VisionaryFX-Linux
          path: |
            electron-app/dist/*.AppImage
            electron-app/dist/*.deb
          retention-days: 30
