name: GBCI

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build:
    runs-on: gbo

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

      - uses: actions/checkout@v4

      - name: Checkout botlib dependency
        uses: actions/checkout@v4
        with:
          repository: GeneralBots/botlib
          path: ../botlib

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

      - name: Install Rust
        uses: msrd0/rust-toolchain@v1
        with:
          toolchain: stable
          targets: |
            x86_64-unknown-linux-gnu
            aarch64-unknown-linux-gnu
            x86_64-pc-windows-gnu
            aarch64-pc-windows-msvc
            x86_64-apple-darwin
            aarch64-apple-darwin

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

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

      - name: Build Linux x86_64
        run: |
          cargo build --locked --release --target x86_64-unknown-linux-gnu

      - name: Build Linux ARM64
        env:
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        run: |
          cargo build --locked --release --target aarch64-unknown-linux-gnu

      - name: Build Windows x86_64
        env:
          CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER: x86_64-w64-mingw32-gcc
        run: |
          cargo build --locked --release --target x86_64-pc-windows-gnu

      - name: Build macOS x86_64 (cross-compile)
        run: |
          cargo build --locked --release --target x86_64-apple-darwin || echo "macOS x86_64 cross-compile requires osxcross"

      - name: Build macOS ARM64 (cross-compile)
        run: |
          cargo build --locked --release --target aarch64-apple-darwin || echo "macOS ARM64 cross-compile requires osxcross"

      - name: Organize release artifacts
        run: |
          mkdir -p release/linux-x86_64
          mkdir -p release/linux-arm64
          mkdir -p release/windows-x86_64
          mkdir -p release/macos-x86_64
          mkdir -p release/macos-arm64

          # Linux x86_64
          cp ./target/x86_64-unknown-linux-gnu/release/botui release/linux-x86_64/ || true

          # Linux ARM64
          cp ./target/aarch64-unknown-linux-gnu/release/botui release/linux-arm64/ || true

          # Windows x86_64
          cp ./target/x86_64-pc-windows-gnu/release/botui.exe release/windows-x86_64/ || true

          # macOS x86_64
          cp ./target/x86_64-apple-darwin/release/botui release/macos-x86_64/ || true

          # macOS ARM64
          cp ./target/aarch64-apple-darwin/release/botui release/macos-arm64/ || true

      - name: Deploy binaries
        run: |
          sudo mkdir -p /opt/gbo/releases/botui/linux-x86_64
          sudo mkdir -p /opt/gbo/releases/botui/linux-arm64
          sudo mkdir -p /opt/gbo/releases/botui/windows-x86_64
          sudo mkdir -p /opt/gbo/releases/botui/macos-x86_64
          sudo mkdir -p /opt/gbo/releases/botui/macos-arm64

          sudo cp -r release/* /opt/gbo/releases/botui/
          sudo chmod -R 755 /opt/gbo/releases/botui/

      - name: Deploy and restart local service
        run: |
          lxc exec bot:pragmatismo-system -- systemctl stop botui

          sudo cp ./target/x86_64-unknown-linux-gnu/release/botui /opt/gbo/bin/botui
          sudo chmod +x /opt/gbo/bin/botui

          lxc exec bot:pragmatismo-system -- systemctl start botui
