name: GBCI Bundle

on:
  push:
    branches: ["main"]
    tags: ["v*"]
  pull_request:
    branches: ["main"]
  workflow_dispatch:

env:
  CARGO_HOME: /root/.cargo
  PATH: /root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

jobs:
  build-bundle:
    if: false # Workflow disabled - keep file for reference
    runs-on: gbo

    steps:
      - name: Disable SSL verification (temporary)
        run: git config --global http.sslVerify false

      - uses: actions/checkout@v4

      - name: Clone dependencies
        run: |
          git clone --depth 1 https://github.com/GeneralBots/botlib.git ../botlib
          git clone --depth 1 https://github.com/GeneralBots/botui.git ../botui
          git clone --depth 1 https://github.com/GeneralBots/botbook.git ../botbook
          git clone --depth 1 https://github.com/GeneralBots/botmodels.git ../botmodels

      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-bundle-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-bundle-

      - name: Install Rust
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          echo "/root/.cargo/bin" >> $GITHUB_PATH
          /root/.cargo/bin/rustup target add x86_64-unknown-linux-gnu
          /root/.cargo/bin/rustup target add aarch64-unknown-linux-gnu
          /root/.cargo/bin/rustup target add x86_64-pc-windows-gnu

      - name: Install cross-compilation dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            gcc-mingw-w64-x86-64 \
            gcc-aarch64-linux-gnu \
            libc6-dev-arm64-cross \
            zip

      - name: Setup environment
        run: sudo cp /opt/gbo/bin/system/.env .

      # ============================================
      # Download Python portable distributions
      # ============================================
      - name: Download Python portables
        run: |
          PYTHON_VERSION="3.12.7"
          mkdir -p python-installers

          # Linux x86_64 - python-build-standalone
          curl -L -o python-installers/python-${PYTHON_VERSION}-linux-x86_64.tar.gz \
            "https://github.com/indygreg/python-build-standalone/releases/download/20241016/cpython-${PYTHON_VERSION}+20241016-x86_64-unknown-linux-gnu-install_only.tar.gz"

          # Linux ARM64 - python-build-standalone
          curl -L -o python-installers/python-${PYTHON_VERSION}-linux-arm64.tar.gz \
            "https://github.com/indygreg/python-build-standalone/releases/download/20241016/cpython-${PYTHON_VERSION}+20241016-aarch64-unknown-linux-gnu-install_only.tar.gz"

          # Windows x86_64 - python-build-standalone
          curl -L -o python-installers/python-${PYTHON_VERSION}-windows-x86_64.tar.gz \
            "https://github.com/indygreg/python-build-standalone/releases/download/20241016/cpython-${PYTHON_VERSION}+20241016-x86_64-pc-windows-msvc-install_only.tar.gz"

          # macOS x86_64 - python-build-standalone
          curl -L -o python-installers/python-${PYTHON_VERSION}-macos-x86_64.tar.gz \
            "https://github.com/indygreg/python-build-standalone/releases/download/20241016/cpython-${PYTHON_VERSION}+20241016-x86_64-apple-darwin-install_only.tar.gz"

          # macOS ARM64 - python-build-standalone
          curl -L -o python-installers/python-${PYTHON_VERSION}-macos-arm64.tar.gz \
            "https://github.com/indygreg/python-build-standalone/releases/download/20241016/cpython-${PYTHON_VERSION}+20241016-aarch64-apple-darwin-install_only.tar.gz"

          ls -la python-installers/

      # ============================================
      # Build botserver for all platforms
      # ============================================
      - name: Build botserver - Linux x86_64
        run: /root/.cargo/bin/cargo build --release --locked --target x86_64-unknown-linux-gnu

      - name: Build botserver - Linux ARM64
        env:
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        run: /root/.cargo/bin/cargo build --release --locked --target aarch64-unknown-linux-gnu

      - name: Build botserver - Windows x86_64
        env:
          CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER: x86_64-w64-mingw32-gcc
        run: /root/.cargo/bin/cargo build --release --locked --target x86_64-pc-windows-gnu

      # ============================================
      # Build botui for all platforms
      # ============================================
      - name: Build botui - Linux x86_64
        run: |
          cd ../botui
          /root/.cargo/bin/cargo build --release --locked --target x86_64-unknown-linux-gnu

      - name: Build botui - Linux ARM64
        env:
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        run: |
          cd ../botui
          /root/.cargo/bin/cargo build --release --locked --target aarch64-unknown-linux-gnu

      - name: Build botui - Windows x86_64
        env:
          CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER: x86_64-w64-mingw32-gcc
        run: |
          cd ../botui
          /root/.cargo/bin/cargo build --release --locked --target x86_64-pc-windows-gnu

      # ============================================
      # Build botbook documentation
      # ============================================
      - name: Install mdBook
        run: |
          if ! command -v mdbook &> /dev/null; then
            /root/.cargo/bin/cargo install mdbook
          fi

      - name: Build botbook
        run: |
          cd ../botbook
          mdbook build

      # ============================================
      # Create bundle directories
      # Structure:
      #   botserver(.exe)           <- root binary
      #   botserver-components/
      #     botui(.exe)
      #     botmodels/
      #     botbook/
      #   botserver-installers/
      #     python-<version>-<platform>.tar.gz
      #     postgresql, valkey, vault, minio, zitadel, llama, models...
      # ============================================
      - name: Create bundle structure
        run: |
          BUNDLE_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          PYTHON_VERSION="3.12.7"

          # Linux x86_64 bundle
          mkdir -p bundle/linux-x86_64/botserver-components
          mkdir -p bundle/linux-x86_64/botserver-installers
          cp ./target/x86_64-unknown-linux-gnu/release/botserver bundle/linux-x86_64/botserver || true
          cp ../botui/target/x86_64-unknown-linux-gnu/release/botui bundle/linux-x86_64/botserver-components/ || true
          cp python-installers/python-${PYTHON_VERSION}-linux-x86_64.tar.gz bundle/linux-x86_64/botserver-installers/

          # Linux ARM64 bundle
          mkdir -p bundle/linux-arm64/botserver-components
          mkdir -p bundle/linux-arm64/botserver-installers
          cp ./target/aarch64-unknown-linux-gnu/release/botserver bundle/linux-arm64/botserver || true
          cp ../botui/target/aarch64-unknown-linux-gnu/release/botui bundle/linux-arm64/botserver-components/ || true
          cp python-installers/python-${PYTHON_VERSION}-linux-arm64.tar.gz bundle/linux-arm64/botserver-installers/

          # Windows x86_64 bundle
          mkdir -p bundle/windows-x86_64/botserver-components
          mkdir -p bundle/windows-x86_64/botserver-installers
          cp ./target/x86_64-pc-windows-gnu/release/botserver.exe bundle/windows-x86_64/botserver.exe || true
          cp ../botui/target/x86_64-pc-windows-gnu/release/botui.exe bundle/windows-x86_64/botserver-components/ || true
          cp python-installers/python-${PYTHON_VERSION}-windows-x86_64.tar.gz bundle/windows-x86_64/botserver-installers/

      # ============================================
      # Copy shared components to all bundles
      # ============================================
      - name: Copy botmodels to bundles
        run: |
          for platform in linux-x86_64 linux-arm64 windows-x86_64; do
            mkdir -p bundle/$platform/botserver-components/botmodels
            cp -r ../botmodels/* bundle/$platform/botserver-components/botmodels/
          done

      - name: Copy botbook to bundles
        run: |
          for platform in linux-x86_64 linux-arm64 windows-x86_64; do
            mkdir -p bundle/$platform/botserver-components/botbook
            cp -r ../botbook/book/* bundle/$platform/botserver-components/botbook/
          done

      - name: Copy installers to bundles
        run: |
          for platform in linux-x86_64 linux-arm64 windows-x86_64; do
            cp -r ./botserver-installers/* bundle/$platform/botserver-installers/
          done

      # ============================================
      # Create ZIP archives
      # ============================================
      - name: Create release archives
        run: |
          BUNDLE_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')

          cd bundle
          for platform in linux-x86_64 linux-arm64 windows-x86_64; do
            if [ -f "$platform/botserver" ] || [ -f "$platform/botserver.exe" ]; then
              zip -r "botserver-bundle-${BUNDLE_VERSION}-${platform}.zip" "$platform"
              echo "Created: botserver-bundle-${BUNDLE_VERSION}-${platform}.zip"
            fi
          done
          cd ..

      # ============================================
      # Deploy bundles
      # ============================================
      - name: Deploy bundle releases
        run: |
          BUNDLE_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')

          sudo mkdir -p /opt/gbo/releases/botserver-bundle
          sudo cp bundle/*.zip /opt/gbo/releases/botserver-bundle/ || true

          # Also keep unpacked bundles for direct access
          sudo mkdir -p /opt/gbo/releases/botserver-bundle/unpacked
          sudo cp -r bundle/linux-x86_64 /opt/gbo/releases/botserver-bundle/unpacked/ || true
          sudo cp -r bundle/linux-arm64 /opt/gbo/releases/botserver-bundle/unpacked/ || true
          sudo cp -r bundle/windows-x86_64 /opt/gbo/releases/botserver-bundle/unpacked/ || true

          sudo chmod -R 755 /opt/gbo/releases/botserver-bundle/

          echo "Bundle releases deployed to /opt/gbo/releases/botserver-bundle/"
          ls -la /opt/gbo/releases/botserver-bundle/
